In cakephp, adding relationships is easy. We just have to declare the hasOne, belongsTo, hasMany and the famous HABTM relationship to do all the dirty work of joining tables. But declaring them all at once could lead to a slow page.
In version 1.1, what we do is utilize the bindModel() and unbindModel(). We do not declare all associations in the Model class yet. This was a good solution. I was looking for this option in version 1.2 and I found the Containable Behavior. Studying containable was a little rough before but there are documents now that are really helpful.
Today, I’ll be showing you how to use Containable in pagination and in regular find method. You must have a fair understanding of the behavior so I suggest you read the above articles first. If you are ready, follow the instructions below:
The idea of the sample is taken from a virtual school application. So we have Departments, Courses, Classes, Students and Teachers, Students Comments on Class and Students Ratings on Class. The following is an explanation of tables declared in class.sql so you can follow the relationships:
TABLE
RELATIONSHIP
department
has many courses
courses
has many classes
belongsto departments
classes
belongs to courses
belongs to teacher
has many comments
has many ratings
teachers
has many classes
ratings
belongs to class
belongs to student
comments
belongs to class
belongs to student
students
has many comments
has many ratings
Exampe1: List of Classes
What we will do is get only the basic information for the list of classes. Those includes:
It will look like this (replace the http://localhost with your local address)
http://localhost/cake/my_classes/index
Let’s compare the recursive and containable method.
Using The Recursive Method
Check the MyClassesController::unfiltered_result() for your reference. First try is I added the ffg line
$this->MyClass->recursive = 1;
but the problem is that I could not get the Department name. Then I tried
$this->MyClass->recursive = 2;
and there I have all the information I need. The problem is that there are just to many data I do not need. Another option is to use the the paginate fields filter. Try adding this:
$this->paginate['MyClass']['fields'] = array( 'Course.title', 'MyClass.title' );
The result? Check it for yourself in http://localhost/cake/my_classes/unfiltered_result
Using Containable Behavior
If you look at MyClassesController::index2(), you would see the ffg:
$this->paginate['MyClass']['contain'] = array (
'Teacher',
'Course' => array (
'Department' => array (
'fields' => array (
'Department.title',
'Department.id'
)
)
)
);
That basically tells cake to find the Class Teacher and return all fields, the course it belongs to and return all the fields and finally the course’ Department title and ID. Now run this http://localhost/cake/my_classes/filtered_result and compare the result with the previous example. It became cleaner isn’t it?
With Containable, you can specify which tables to include and what fields to include each table. You can also add order and limit filters and also conditions. It took me a while to grasp the format because at first glance it is a bit difficult to follow. I use the following format:
$this->Model0->contain( array(
'Model1' => array (
'fields' => array( 'modelField1', 'modelField2' ),
'conditions' => array( 'your condition here' ),
'order' => array( 'id' => 'asc' ),
),
'Model2',
'Model2'
)
)
Example2: Class View
This is just to show you a more complicated containable query. We want to get more information about the class. That includes:
We can achieve the desired results using recursive but then again we will be wasting so much process time. In containable, we’ll just have to declare what we want.
$this->MyClass->contain( array(
'Teacher',
'Rating' => array(
'Student',
'order' => array( 'created' => 'desc' ),
'conditions' => array( 'approved' => 1 )
),
'Comment' => array(
'Student',
'order' => array( 'RAND()' ),
'limit' => 3 ,
'conditions' => array( 'approved' => 1 )
),
'Course' => array(
'Department' => array(
'fields' => array(
'Department.title',
'Department.id'
)
)
)
)
);
Can you see the format now? Play around with the codes and check their results to get more comfortable with it. Happy Baking!