Phalcon\Mvc\Model::query(): Syntax error
Code:
Phalcon\Mvc\Model::query()
->where('id = ' . (int) $id)
->order('order')
->limit($limit, $offset)
->execute();
Output:
object(Phalcon\Mvc\Model\Exception)#40 (7) {
["message":protected]=>
string(58) "Syntax error, unexpected token ORDER, near to ' LIMIT 100'"
...
Escaping column name:
Phalcon\Mvc\Model::query()
->where('id = ' . (int) $id)
->order('`order`')
->limit($limit, $offset)
->execute();
Output:
object(Phalcon\Mvc\Model\Exception)#40 (7) {
["message":protected]=>
string(42) "Parsing error before 'order` LIMIT 100...'"
...
This is not a bug, 'order' is a PHQL reserved word, you need to use the PHQL escaper delimiters [ and ] to escape this field:
Phalcon\Mvc\Model::query()
->where('id = ' . (int) $id)
->order('[order]')
->limit($limit, $offset)
->execute();
http://docs.phalconphp.com/en/latest/reference/phql.html#escaping-reserved-words
Or you could use the column map to change the name of the field in your model
public function columnMap()
{
//Keys are the real names in the table and
//the values their names in the application
return array(
'order' => 'myorder'
);
}
and your code becomes
Phalcon\Mvc\Model::query()
->where('id = ' . (int) $id)
->order('myorder')
->limit($limit, $offset)
->execute();
Thank you!
I also have a question. Tell me how to use the Phalcon\Mvc\Model and PHQL affects performance in comparison with pure PDO?
PHQL represents more advantages than disadvantages, overall loss in performance is really minimal since the parser and PHQL processor are written in C and the whole execution is performed without consume any external process or daemon, they're just compiled functions in the same extension. Usually, in an application, the bottlenecks are the connections to the database and the entire communication process required to send/receive data from the database system. Also, reading/writing the filesystem is another common cause of downgrading in performance. That is the reason why it is recommended adding some caching layer where it is needed.
http://docs.phalconphp.com/en/1.0.0/reference/models-cache.html
Thank you!