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

CRUD operations using ORM

By using the ORM, there is virtually no need to write any SQL in your code. Everything is OOP, and it is using the models to perform operations. The first, and the most basic, operation is retrieving data. In the old days, you would do this:

$result = mysql_query("SELECT * FROM article");

The class that our models are extending is \Phalcon\Mvc\Model. This class has some very useful methods built in, such as find(), findFirst(), count(), sum(), maximum(), minimum(), average(), save(), create(), update(), and delete().

CRUD – reading data

We have already used the find() method in our article manager when calling Article::find(). By default, this method will return all the records from the article table, sorting them in a natural order. It also accepts an array with parameters. The following code examples will explain this:

$article_slug = "test-article-short-title";

$result = Article::find(
        "article_slug = :article_slug:",
        "bind" => ["article_slug...