Tables are not escaped properly
I have table like, that describes entity like in my project.
This code throws fatal error:
$like = Like::findFirst(1);
var_dump($like);
// Syntax error, unexpected token LIKE, near to .* FROM Like WHERE Like.id = 1 LIMIT 1
I suppose if table names will be wrapped by " ` " symbol, this problem will be solved.
Wow! one more issue here.
in my model Like i have defined method getSource() with different table name and problem is reproduced. It seems phalcon tries to take Model name as table name, even if i have defined getSource() method:
class Like extends \Phalcon\Mvc\Model
{
public function getSource()
{
return 'user_like';
}
}
try
protected $_source = 'user_like';
Doesn't help. findFirst() is static method, so it can't get non-static property value. Maybe problem is there?
As for created object, situation is the same:
$like = new Like();
$like->findFirst(1); // gives same exception
Hi avasin, just added a fix for this in 0.8.0, however, note that the escaping system help you to escape most of the SQL reserved words. There are more than 80 reserved words in most popular database systems. PHQL only has about 22 and one of them is 'Like'. PHQL reserved words can't be escaped by the ORM itself because they make the parser works correctly.
Starting from 0.8.0, the special delimiters [ and ] are available to escape PHQL reserved words. These delimiters are translated internally to the corresponding escape characters of each database system.
Finally I must say that using a SQL reserved word will make your life less easy because you have to escape it manually in some parts of the code. ;)
Hello phalcon,
Thank you for your answer, and for fix :)
But, as i understand there is different problem: Phalcon\Mvc\Model::findFirst() method doesn't use table name:
- specified in $_source property
- returned by getSource() model method
More over, logically retrieving table name from non-static property and non-static method should be not possible by static method. So, in result (as i understood), phalcon framework tries here to take model class name as table name, which is not suitable here.
I really love that phalcon is as unbeliveable fast as unbelieveable intelligent, and tries to handle many things itself, but what is happening here seems strange - i can have deep namespacing in models, or sometimes i need to have some separate models for one table (so their names will be not same as table name).
So, main question is - if i define table name in _source property or in getSource() method - should all Model static methods use my defined table name? If there is a way to define table name, that will be used as by static methods as by all other - it's fine, please just tell me how i can do this :)
As for reserved words - i can agree with you, but in some cases it's much more usable to call entity with one word, that will be clear and easy to understand - this saves many developers' time. And with proper escaping here should be no problems :)
Hey Alexander, I can't reproduce your problem, I created this gist with the issue you're describing: https://gist.github.com/4334639
The gist is running here: http://test.phalconphp.com/like.php, please let me know if I need to change something on it, thanks
Had same issue with Order table.
Updated to 0.8.0.
Now Order::find() works ok. Thank you!
But another issue with PHQL:
$query = $this->modelsManager->createQuery("SELECT * FROM Order");
$orders = $query->execute();
Syntax error, unexpected EOF
In version 0.7.0 there was the same error, when I added backticks manually.
When I put
$query = $this->modelsManager->createQuery("SELECT * FROM Order WHERE id=1");
$orders = $query->execute();
Syntax error, unexpected token ORDER, near to ' WHERE id=1'
This might be related to the name of your table. Can you change your class to be something like Orders and try it?
I am guessing that the parser is confused by the ORDER which is a reserved word in the RDBMS.
You can use the cross-database delimiters provided by PHQL '[' and ']':
$query = $this->modelsManager->createQuery("SELECT * FROM [Order] WHERE id=1");
$orders = $query->execute();