cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Phalcon\Mvc\Model::query(): Syntax error

Open nikolasha opened this issue 13 years ago • 5 comments

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...'"
...

nikolasha avatar Feb 19 '13 15:02 nikolasha

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

ghost avatar Feb 19 '13 18:02 ghost

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();

niden avatar Feb 19 '13 18:02 niden

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?

nikolasha avatar Feb 19 '13 22:02 nikolasha

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

ghost avatar Feb 20 '13 01:02 ghost

Thank you!

nikolasha avatar Feb 20 '13 11:02 nikolasha