We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



85.5k
Accepted
answer

'order' => ' [end] DESC'



2.9k

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?



85.5k

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



2.9k

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