<?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>Rick Guyer .com &#187; model</title>
	<atom:link href="http://rickguyer.com/tag/model/feed/" rel="self" type="application/rss+xml" />
	<link>http://rickguyer.com</link>
	<description>Information about the web, web development, and technology.</description>
	<lastBuildDate>Wed, 21 Sep 2011 13:50:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Speeding up count queries with counterCache</title>
		<link>http://rickguyer.com/speeding-up-count-queries-with-countercache/</link>
		<comments>http://rickguyer.com/speeding-up-count-queries-with-countercache/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 01:18:42 +0000</pubDate>
		<dc:creator>ricog</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://ricoguyer.com/?p=8</guid>
		<description><![CDATA[Today while working in a CakePHP application, I needed to hide some records based on the number of associated records they had. I basically needed to get a count of associated records. Fortunately, CakePHP has a feature called counterCache that makes this really easy. It saves the record count in the database and updates the [...]]]></description>
			<content:encoded><![CDATA[<p>Today while working in a <a href="http://cakephp.org">CakePHP</a> application, I needed to hide some records based on the number of associated records they had. I basically needed to get a count of associated records. Fortunately, CakePHP has a feature called <a href="http://book.cakephp.org/view/490/counterCache-Cache-your-count">counterCache</a> that makes this really easy. It saves the record count in the database and updates the count any time you do a save or delete. The details can be found here:  <a href="http://book.cakephp.org/view/490/counterCache-Cache-your-count">http://book.cakephp.org/view/490/counterCache-Cache-your-count</a></p>
<p>Basically you do the following:</p>
<ol>
<li>Create an INT field on the parent table called &lt;childmodelname&gt;_count</li>
<li>In the $belongsTo array of the child model, add &#8220;&#8216;counterCache&#8217; =&gt; true&#8221; to the parent array</li>
<li>Enjoy!</li>
</ol>
<p>This works great for new installations. However, if you want to add it to an existing site, you&#8217;ll need to run through all the records and save the current count. Here is a sample method for doing just that.</p>
<pre class="brush: php">
function admin_update_counters() {
$this-&gt;Category-&gt;recursive = 0;
$categories = $this-&gt;Category-&gt;find(&#039;all&#039;);
foreach ($categories as $category) {
$question_count = $this-&gt;Category-&gt;Question-&gt;find(&#039;count&#039;, array(&#039;conditions&#039; =&gt; array(&#039;Question.category_id&#039; =&gt; $category[&#039;Category&#039;][&#039;id&#039;])));
if ($question_count) {
$this-&gt;Category-&gt;create();
$this-&gt;Category-&gt;id = $category[&#039;Category&#039;][&#039;id&#039;];
$this-&gt;Category-&gt;saveField(&#039;question_count&#039;, $question_count);
}
}

echo &quot;Counters updated&quot;; exit;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rickguyer.com/speeding-up-count-queries-with-countercache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

