Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon\Mvc\Model\Exception: Syntax error, unexpected token END

Hello. I have a database field that is called "end" and contains a UNIX timestamp. However, when I try to order by that field like this:

$Project = \Common\Models\Projects::findFirst([
        'conditions' => 'id=?0 AND source_id=1',
        'bind' => [$projectId],
        'order' => 'end DESC'
]);

Phalcon throws this exception:

Phalcon\Mvc\Model\Exception: Syntax error, unexpected token END, near to ' DESC LIMIT :APL0:', when parsing: SELECT [Common\Models\Projects].* FROM [Common\Models\Projects] WHERE id=?0 AND source_id=1 ORDER BY end DESC LIMIT :APL0: (136)

Is there a solution to this issue? In a test environment, I renamed the "end" field in the database and ran the same query, and it worked, but I would much rather keep the name of the field if possible. Please advise.



78.9k
Accepted
answer

'order' => ' [end] DESC'



1.1k

Thank you so much!

Any idea as to why Phalcon doesn't like database fields named "end"? Is this a bug in Phalcon, or is this expected behavior?



78.9k

its mysql reserved world, in pdo escaping is done with []. Its not phalcon related at all , its just the mysql driver



1.1k

Oh, I see. Thanks for the help and further explanation.