Book Image

Learning Phalcon PHP

Book Image

Learning Phalcon PHP

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using raw SQL


Sometimes, using raw SQL is the only way of performing complex queries. Let's see what a raw SQL will look like for a custom find() method and a custom update() method :

<?php

use Phalcon\Mvc\Model\Resultset\Simple as Resultset;

class Article extends Base
{
    public static function rawFind()
    {

        $sql     = "SELECT * FROM robots WHERE id > 0";
        $article = new self();

        return new Resultset(null, $article, $article->getReadConnection()->query($sql));
    }

    public static function rawUpdate()
    {
        $sql = "UPDATE article SET is_published = 1";
        $this->getReadConnection()->execute($sql);
    }
}

As you can see, the rawFind() method returns an instance of \Phalcon\Mvc\Model\Resultset\Simple. The rawUpdate() method just executes the query (in this example, we will mark all the articles as published). You might have noticed the getReadConnection() method. This method is very useful when you need to iterate over a large...