<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Promet CakePHP Source &#187; containable</title>
	<atom:link href="http://cakephp.prometsupport.com/tag/containable/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakephp.prometsupport.com</link>
	<description></description>
	<lastBuildDate>Mon, 23 Feb 2009 08:03:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Learn Containable Behavior by Example</title>
		<link>http://cakephp.prometsupport.com/2008/learn-containable-behavior-by-example/</link>
		<comments>http://cakephp.prometsupport.com/2008/learn-containable-behavior-by-example/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 07:58:49 +0000</pubDate>
		<dc:creator>rachel</dc:creator>
				<category><![CDATA[Associations]]></category>
		<category><![CDATA[Behaviors]]></category>
		<category><![CDATA[CakePHP 1.2]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[Tips & Tutorials]]></category>
		<category><![CDATA[containable]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://cakephp.prometsupport.com/?p=44</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>In version 1.1, what we do is utilize the <a href="http://cakephp.prometsupport.com/2008/binding-ur-models-d-easy-way/">bindModel() and unbindModel()</a>. 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.<br />
<span id="more-44"></span></p>
<ul>
<li><a href="http://book.cakephp.org/view/474/Containable">Cakephp Book: Containable</a></li>
<li><a href="http://cakebaker.42dh.com/2008/05/18/new-core-behavior-containable/">CakeBaker: New Core Behavior &#8211; Containable</a></li>
<li><a href="http://www.studiocanaria.com/articles/cakephp_containable_behavior">StudioCanaria: Cakephp Containable Behavior</a></li>
</ul>
<p>Today, I&#8217;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:</p>
<ol>
<li>Download the latest 1.2 release of cake. As of this writing, I tested it in cake_1.2.0.7296-rc2. Do the usual cake installation procedures.</li>
<li><a href="http://cakephp.prometsupport.com/wp-content/uploads/2008/09/containable_by_example.zip">Download this file</a> and unzip. It must contain an &#8220;app&#8221; folder</li>
<li>Replace the /app from #1 with the /app from #2 but leave the config file of #1.</li>
<li>Use the /app/class.sql to setup your database</li>
</ol>
<p>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:</p>
<table style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="10" width="100%">
<tbody>
<tr valign="top">
<th width="20%">TABLE</th>
<th>RELATIONSHIP</th>
</tr>
<tr valign="top">
<td>department</td>
<td>has many courses</td>
</tr>
<tr valign="top">
<td>courses</td>
<td>has many classes<br />
belongsto departments</td>
</tr>
<tr valign="top">
<td>classes</td>
<td>belongs to courses<br />
belongs to teacher<br />
has many comments<br />
has many ratings</td>
</tr>
<tr valign="top">
<td>teachers</td>
<td>has many classes</td>
</tr>
<tr valign="top">
<td>ratings</td>
<td>belongs to class<br />
belongs to student</td>
</tr>
<tr valign="top">
<td>comments</td>
<td>belongs to class<br />
belongs to student</td>
</tr>
<tr valign="top">
<td>students</td>
<td>has many comments<br />
has many ratings</td>
</tr>
</tbody>
</table>
<h2>Exampe1: List of Classes</h2>
<p>What we will do is get only the basic information for the list of classes. Those includes:</p>
<ul>
<li>Class title</li>
<li>Course it belongs to</li>
<li>Department it belongs to</li>
<li>Teacher name</li>
</ul>
<p>It will look like this (replace the http://localhost with your local address)<br />

http://localhost/cake/my_classes/index</p>

<p>Let&#8217;s compare the recursive and containable method.</p>
<p><strong>Using The Recursive Method</strong><br />
Check the MyClassesController::unfiltered_result() for your reference. First try is I added the ffg line<br />
<code lang="php">$this->MyClass->recursive = 1; </code><br />
but the problem is that I could not get the Department name. Then I tried<br />
<code lang="php">$this->MyClass->recursive = 2; </code><br />
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:<br />
<code lang="php">$this->paginate['MyClass']['fields'] = array( 'Course.title', 'MyClass.title' );</code></p>
<p>The result? Check it for yourself in http://localhost/cake/my_classes/unfiltered_result</p>
<p><strong>Using Containable Behavior</strong><br />
If you look at MyClassesController::index2(), you would see the ffg:<br />
<code lang="php"><br />
        $this->paginate['MyClass']['contain'] = array (<br />
            'Teacher',<br />
            'Course' => array (<br />
                'Department' => array (<br />
                    'fields' => array (<br />
                        'Department.title',<br />
                        'Department.id'<br />
                    )<br />
                )<br />
            )<br />
        );<br />
</code><br />
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&#8217; 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&#8217;t it?</p>
<p>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:</p>
<p><code lang="php"><br />
$this->Model0->contain( array(<br />
 'Model1' => array (<br />
 'fields' 	 => array( 'modelField1', 'modelField2' ),<br />
 'conditions' => array( 'your condition here' ),<br />
 'order' 	 => array( 'id' => 'asc' ),<br />
 ),<br />
 'Model2',<br />
 'Model2'<br />
 )<br />
)<br />
</code></p>
<h2>Example2: Class View</h2>
<p>This is just to show you a more complicated containable query. We want to get more information about the class. That includes:</p>
<ul>
<li>Class title</li>
<li>Course it belongs to</li>
<li>Department it belongs to</li>
<li>Teacher name</li>
<li><strong>Student comments that were approved and student name</strong></li>
<li><strong>Student ratings that were approved and student name</strong></li>
</ul>
<p>We can achieve the desired results using recursive but then again we will be wasting so much process time. In containable, we&#8217;ll just have to declare what we want.</p>
<p><code lang="php"><br />
        $this->MyClass->contain( array(<br />
            'Teacher',<br />
            'Rating'     => array(<br />
                'Student',<br />
                'order' => array( 'created' => 'desc' ),<br />
                'conditions' => array( 'approved' => 1 )<br />
            ),<br />
            'Comment'    => array(<br />
                'Student',<br />
                'order' => array( 'RAND()' ),<br />
                'limit' => 3 ,<br />
                'conditions' => array( 'approved' => 1 )<br />
            ),<br />
            'Course'     => array(<br />
                'Department' => array(<br />
                    'fields' => array(<br />
                        'Department.title',<br />
                        'Department.id'<br />
                    )<br />
                )<br />
            )<br />
            )<br />
        );<br />
</code></p>
<p>Can you see the format now? Play around with the codes and check their results to get more comfortable with it. Happy Baking!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakephp.prometsupport.com/2008/learn-containable-behavior-by-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
