Book Image

Learning Phalcon PHP

By : Calin Rada, Ioan C Rada
Book Image

Learning Phalcon PHP

By: Calin Rada, Ioan C Rada

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
About the Author
About the Reviewers

Using PHQL

Personally, I am not a fan of PHQL. I prefer using ORM or Raw queries. But if you are going to feel comfortable with it, feel free to use it. PHQL is quite similar to writing raw SQL queries. The main difference is that you will need to pass a model instead of a table name, and use a models manager service or directly call the \Phalcon\Mvc\Model\Query class. Here is a method similar to the built-in find() method:

public function find()
    $query = new \Phalcon\Mvc\Model\Query("SELECT * FROM App\Core\Models\Article", $this->getDI());
    $articles = $query->execute();
return $articles;

To use the models manager, we need to inject this new service. Open the global services file, config/service.php, and add the following code:

$di['modelsManager'] = function () {
    return new \Phalcon\Mvc\Model\Manager();

Now let's rewrite the find() method by making use of the modelsManager service:

public function find()
    $query = $this->modelsManager->createQuery("SELECT...