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

Escaping special keywords in PHQL

In MySQL I can just do something like the following:

SELECT * FROM treelist ORDER BY `left` ASC;

but if I try to do the following in Phalcon I get an error:

TreeList::find(['order' => '`left` ASC']);

What's the proper way to escape special keywords like "left" and "right" in PHQL?

Also, some unrelated questions:

  • Is Model->afterFetch() broken in 2.0.7 or am I doing something wrong? I tried code which looks similar to the following but it seems afterFetch() wasn't even called (beforeSave() works fine though).
class Robots extends Model
{
    public $id;

    public $part_id;

    public function afterFetch()
    {
        if (empty($this->part_id))
            $this->part_id = -1;
    }
}

print_r(Robots::find()->toArray());
  • What's the best way to strip tags from all fields before saving model? Should I do something like the following or is there a better way?
    public function beforeSave()
    {
        $filter = new Filter();
        $fields = $this->columnMap();
        foreach($fields as $field)
            $this->{$field} = $filter->sanitize($this->{$field}, "striptags");
    }
edited Sep '15

Escaping reserved words is explained in the docs: https://docs.phalcon.io/en/latest/reference/phql.html#escaping-reserved-words

Sorry for not looking at that page. I only checked Model documentation.

Should I create new threads for the other questions or can you quickly answer them here?

Should I create new threads for the other questions or can you quickly answer them here?

I think it would be better :)