Retrieving data using the models
We will start by implementing the menu page. The initial functionality of the menu page will be to retrieve a list of all the milkshakes in alphabetical order.
To retrieve a result set, we have to initialize a Criteria
object and customize it. This object then represents an SQL query. As our custom classes extend the base class, we use one of a number of methods of the parent. Passing the Criteria
object to the method results in the query that is performed on our database. The Criteria
object can handle both simple and relatively complicated queries. However, as a rule it is a good practice to customize the Criteria
object to only retrieve what you want. This will be covered in detail in Chapter 9,Optimizing for performance.
Opening the lib/model/MilkShakePeer.php
file reveals that there are no methods. As this is a custom class, it is up to us to add our business logic. Here we need to create a static method to retrieve data about all the milkshakes from the database in alphabetical order. Let's begin by adding a getAllShakes()
method, which I have done in the following code snippet:
/** * Get all of the milkshakes * * @param Int $limit total amount to be returned. 0 = unlimited * @return Array Array containing objects */ public static function getAllShakes($limit=0) { //Create the criteria $c = new Criteria(); //Set the limit if(0 > $limit) { $c->setLimit($limit); } //Order by name in ascending order $c->addAscendingOrderByColumn(self::NAME); return self::doSelect($c); }
In this code snippet, we first instantiate the Criteria
object and then customize the criteria by setting a limit, if one is required, using the setLimit()
method. This translates into the SQL Limit
clause. To set an order by
clause, we need to use the Criteria
object's addAscendingOrderByColumn()
method. This method takes the column name with which we want to order the result set as an argument. That's all we need to do to the Criteria
object. Next, we call the doSelect()parent
method and pass it to our Criteria
object.
The doSelect()
method does more than just querying the database. After this function has been called, the results are returned to the method and then the results are converted into our model objects. The whole process is called hydration. Essentially, the following happens in the hydrating process:
- An SQL query is created and optimized for our chosen database.
- All values that we pass to the
Criteria
object are escaped and are safe to use. - The database is queried.
- A result set is returned as an array of objects.
This is a very quick and convenient way of retrieving our data. However, with ease comes danger. It is very easy to fall into the habit of retrieving everything even if we only need one or two columns. We'll look at bypassing the hydration process in Chapter 9,Optimizing for Performance.
Note
Autoloading
You may have noticed that there are no PHP include()
or require()
functions. This is because Symfony implements class Autoloading
. When you first run your application, Symfony registers all the classes, compiles an associative array, and stores it in cache so that it doesn't have to repeat the process. Therefore, when adding new classes, we have to clear the Symfony cache. We will learn to clear the Symfony cache later in this chapter, after installing a plugin.
All our column titles are class constants, so refer to the column as self::COL_NAME
.