<?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; Behaviors</title>
	<atom:link href="http://cakephp.prometsupport.com/category/model/behaviors/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>INI Based Permission Component</title>
		<link>http://cakephp.prometsupport.com/2009/ini-based-permission-component/</link>
		<comments>http://cakephp.prometsupport.com/2009/ini-based-permission-component/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 13:37:42 +0000</pubDate>
		<dc:creator>rachel</dc:creator>
				<category><![CDATA[Behaviors]]></category>
		<category><![CDATA[CakePHP 1.1]]></category>
		<category><![CDATA[CakePHP 1.2]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Helpers]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[Tips & Tutorials]]></category>
		<category><![CDATA[cakePHP websites]]></category>

		<guid isPermaLink="false">http://cakephp.prometsupport.com/?p=142</guid>
		<description><![CDATA[Yet another permission component was created this week but this time, it&#8217;s simplier and written in a text file. In one of our project, one of the follow-up requirement was to allow users of certain groups upgrade into a better status. The project was already in production and I was using ACL with Auth component [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another permission component was created this week but this time, it&#8217;s simplier and written in a text file. In one of our project, one of the follow-up requirement was to allow users of certain groups upgrade into a better status. The project was already in production and I was using ACL with Auth component to do my authentication. Unfortunately, this is one of <a href="http://www.mail-archive.com/cake-php@googlegroups.com/msg57645.html">ACLs weakness</a>. </p>
<blockquote><p>Changing a users group does not change the ARO parent (effectively making it useless/impossible to change groups). I tried fixing this home-brew style, but since there&#8217;s no setParent in 1.2&#8242;s db_acl, it&#8217;s far harder than it should be.</p>
</blockquote>
<p>That was reported back in 2007 and there was still no solution found in the net. It is also not an option for me to go through the above suggestion in fixing the problem so the the other solution is to find another one or create one. I chose the latter.</p>
<p><strong>Why create a new one?</strong></p>
<p>The other solutions that I found still needs some configuration in the controllers. Since I already have a ton of files, I don&#8217;t want to go through all of them to add a line or two. I need ACLs way of doing it all in the background. Also, I am already using Auth component and I want to keep it.</p>
<p><strong>Why did I choose a text file over a database?</strong></p>
<p>My intention is to make the application faster since load time is really getting slower. Since the application isn&#8217;t that big, I only have less than 200 lines for the permissions, so a database is just overkill. Even if I use a database, I might cache it anyway so it saves me extra query.</p>
<p><strong>The component</strong></p>
<p>So Permission Component was created. Below is the instruction on how to install it.</p>
<ol>
<li><a href='http://cakephp.prometsupport.com/wp-content/uploads/2009/01/permissionphp.txt'>Download this component</a> and copy to your components folder. </li>
<li>Create cake_dir/app/config/permissions.ini</li>
</ol>
<p>The content for permissions.ini is as follows</p>
<p><code lang="ini[lines]"><br />
[ControllerName]<br />
actionName = group_id,group_id<br />
 <br />
[PluginName.ControllerName]<br />
actionName = group_id,group_id<br />
</code></p>
<p>The <em>ControllerName</em> should be the controller name itself (in camel-case format).</p>
<p>The <em>actionName</em> is also the action name itself (in camel-case format).</p>
<p>The <em>PluginName</em> is the plugin name itself (in camel-case format).</p>
<p>The <em>group_id</em> is the one written in your Auth component. Your Auth component should have a group_id value because this is what our Permission component will look for. It could be a number or set of characters.</p>
<p>Example:</p>
<p><code lang="ini[lines]"><br />
; Group ID 1 = Admin<br />
; Group ID 2 = Member<br />
[Accounts]<br />
index =<br />
add = 1<br />
edit = 1,2<br />
delete = 1,2<br />
 <br />
[ShoppingCart.Orders]<br />
index =<br />
report = 1<br />
</code></p>
<p>Assigning no group id in an actionName will simply ignore it.</p>
<p><strong>Reminder</strong></p>
<p>Use at your own risk. If you found a bug, please post them at the comment form.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakephp.prometsupport.com/2009/ini-based-permission-component/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<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>
