<?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>Cakephp.nu blog &#187; tutorials</title>
	<atom:link href="http://www.cakephp.nu/t/tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://www.cakephp.nu</link>
	<description>Cake and cookies by Jippi</description>
	<lastBuildDate>Wed, 11 Aug 2010 09:15:50 +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>CounterCache in CakePHP 1.2 beta</title>
		<link>http://www.cakephp.nu/countercache-cakephp-12-beta</link>
		<comments>http://www.cakephp.nu/countercache-cakephp-12-beta#comments</comments>
		<pubDate>Fri, 04 Jan 2008 23:32:54 +0000</pubDate>
		<dc:creator>Christian Winther</dc:creator>
				<category><![CDATA[Cakephp]]></category>
		<category><![CDATA[counter cache]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.cakephp.nu/countercache-cakephp-12-beta</guid>
		<description><![CDATA[
Update 05.01.2008 At the moment the counterCache code is only executed on CREATE, not on UPDATE &#8211; a patch is under way Hello, Just a little turtorial to demonstrate the new model counterCache feature. CounterCache is a very fancy little addition to our beloved CakePHP framework. Simply put, all it does is make sure that [...]
]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong><br />
<em>05.01.2008</em> At the moment the counterCache code is only executed on CREATE, not on UPDATE &#8211; a patch is under way</p>
<p>Hello,</p>
<p>Just a little turtorial to demonstrate the new model counterCache feature.</p>
<p>CounterCache is a very fancy little addition to our beloved CakePHP framework.<br />
Simply put, all it does is make sure that your belongsTo associations always have an updated field with the count of elements belonging to the parent.</p>
<p>Lets use this simple example, just two models and controllers:</p>
<ul>
<li>Posts (posts_controller) (hasMany PostComment)
</li>
<li>PostComments (post_comments_controller) (belongsTo Post)
</li>
</ul>
<h3>SQL</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- our posts </span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`posts`</span> <span style="color: #66cc66;">&#40;</span> 
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`post_comment_count`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`title`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`body`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8 ROW_FORMAT<span style="color: #66cc66;">=</span>DYNAMIC;             
&nbsp;
<span style="color: #808080; font-style: italic;">-- comments to our post </span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`post_comments`</span> <span style="color: #66cc66;">&#40;</span> 
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`post_id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`is_active`</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
  <span style="color: #ff0000;">`body`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`post_id`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`post_id`</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8 ROW_FORMAT<span style="color: #66cc66;">=</span>DYNAMIC;</pre></td></tr></table></div>

<p>To keep things simple, we are just going to use scaffolding in controllers.</p>
<h3>Controllers</h3>
<h4>Posts controller</h4>
<p>Put in APP/controllers/posts_controller.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php 
<span style="color: #666666; font-style: italic;">// Posts controller </span>
<span style="color: #000000; font-weight: bold;">class</span> PostsController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$scaffold</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>PostComments controller</h4>
<p>Put in APP/controllers/post_comments_controller.php</p>
<pre></pre>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php 
<span style="color: #666666; font-style: italic;">// Post comments controller </span>
<span style="color: #000000; font-weight: bold;">class</span> PostCommentsController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$scaffold</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Models</h3>
<h4>Post model</h4>
<p>Put in APP/models/post.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php 
<span style="color: #000000; font-weight: bold;">class</span> Post <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hasMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PostComment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>PostComment model</h4>
<p>Put in APP/models/post_comment.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php 
<span style="color: #000000; font-weight: bold;">class</span> PostComment <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$belongsTo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'counterCache'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The interesting thing is the above <strong>PostComment</strong> model. A you can see, a new options key is added, <strong>counterCache</strong> and is set to <u>(bool) true</u>. This informs cake that it should use counterCache on the class (Post) it belongs to.<br />
This is three possible variables to assign <strong>counterCache</strong></p>
<ul>
<li><strong>(bool) true</strong> means enabled, and that cake should auto-generate the count field automagic
</li>
<li><strong>(string) fieldname</strong> tells cake to use your custom field in Post (posts) model and table.
</li>
<li><strong>any value that is considered &#8216;empty&#8217;</strong>disable the feature (this is default, NULL)
</li>
</ul>
<p>Its important to understand, that the field we specify here, is a field in the REMOTE model / table, not in the local table, so in this case, the field name is in <strong>posts</strong> table.!</p>
<p>As said earlier, cake will automagic generate the remote field name if you only provide a boolean true value, in this case the field will be named <strong>Inflector::underscore($this-&gt;alias) . &#8216;_count&#8217;</strong> &#8211; meaning the alias of the <strong>LOCAL</strong> model (PostComment) and a suffix &#8216;_count&#8217;. All is that is converted to a lowercase, underscored value. When these rules are applied to our example above, the remote field in posts will be named <strong>post_comment_count</strong> (line 4 in SQL section)</p>
<p>With the code above, you have a very basic scaffolding setup running with couterCache enabled. Very simple stuff <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  As always when its from the CakePHP team.</p>
<h3>Using counterCache scope</h3>
<p>As you might have noticed in the SQL table above, the post_comments table have a field called <strong>is_active</strong> (SQL line 14). This is a sample on how you could moderate your comments. Lets change a bit of code in our models to make this work.</p>
<p>Put in APP/models/post.php (Updated)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php 
<span style="color: #666666; font-style: italic;">// Updated Post model </span>
<span style="color: #000000; font-weight: bold;">class</span> Post <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span> 
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hasMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PostComment'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
        <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_active'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that I have added a <strong>conditions</strong> array to the <strong>hasMany</strong> relation to <strong>PostComment</strong>.<br />
This will tell cake to only fetch comments from PostComment where is_active is 1 (aka. is active).</p>
<p>Put in APP/models/post_comment.php (Updated)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php 
<span style="color: #000000; font-weight: bold;">class</span> PostComment <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$belongsTo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
        <span style="color: #0000ff;">'counterCache'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'counterScope'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_active'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that I have added another field to the <strong>Post</strong> relation called <strong>counterScope</strong>, this is the conditions that cake uses in the <strong>find(&#8216;count&#8217;)</strong>query it creates to find the amount of comments to the Post. By adding this simple array, our counterCache now only count active comments <img src='http://www.cakephp.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Easy done</p>

	Tags: <a href="http://www.cakephp.nu/t/cakephp" title="Cakephp" rel="tag">Cakephp</a>, <a href="http://www.cakephp.nu/t/counter-cache" title="counter cache" rel="tag">counter cache</a>, <a href="http://www.cakephp.nu/t/english" title="english" rel="tag">english</a>, <a href="http://www.cakephp.nu/t/tutorials" title="tutorials" rel="tag">tutorials</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.cakephp.nu/countercache-cakephp-12-beta/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc

Served from: www.cakephp.nu @ 2012-02-05 16:58:53 -->
