PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

[FIXED] Why is the table alias converted to lower case in this query expression

 December 31, 2021     cakephp, cakephp-3.0     No comments   

Issue

This query shall get all products with a price reduction exceeding 30%:

return $query->where([
    'Products.reduced_price / Products.price <' => 0.7,
]);

This query results in the following error:

Column not found: 1054 Unknown column 'products.price' in 'where clause'

Why is the table alias converted to lower case? This error seems to depend on your mysql-settings. Some setups seem to be case-insensitive (for instance my dev-machine) others like my production server are case-sensitive ;)

Unfortunately it is not possible to leave out the table-alias "Products" in this condition, because there is a joined table that has also a column named "price". Leaving the alias out would lead to this error: Column 'price' in where clause is ambiguous


Solution

The reason is the _parseCondition method of QueryExpression class. It will assume everything after the first space to be the operator and use strtolower on it, thus making the alias lower-case.

This can be easily mitigated by removing all spaces from the expression like this:

return $query->where([
    'Products.reduced_price/Products.price <' => 0.7,
]);


Answered By - David Albrecht
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing