<?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>Olle Jonsson&#039;s blog &#187; cakephp</title>
	<atom:link href="http://ollehost.dk/blog/tag/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://ollehost.dk/blog</link>
	<description>A human weblog about Web technology and other ephemera</description>
	<lastBuildDate>Mon, 06 Feb 2012 09:22:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CakePHP: grab only the data you need</title>
		<link>http://ollehost.dk/blog/2006/09/21/cakephp-optimization-hint/</link>
		<comments>http://ollehost.dk/blog/2006/09/21/cakephp-optimization-hint/#comments</comments>
		<pubDate>Thu, 21 Sep 2006 11:38:36 +0000</pubDate>
		<dc:creator>olleolleolle</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cakephp]]></category>

		<guid isPermaLink="false">http://olle.ter.dk/blog/?p=304</guid>
		<description><![CDATA[When dealing with an abstraction that outputs dynamic SQL &#8220;behind your back&#8221;, it&#8217;s often easy to forget that some of the queries are&#8230; too greedy and grab unneeded data. Here is how to ask for less data, using CakePHP. Today, &#8230; <a href="http://ollehost.dk/blog/2006/09/21/cakephp-optimization-hint/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When dealing with an abstraction that outputs dynamic SQL &#8220;behind your back&#8221;, it&#8217;s often easy to forget that some of the queries are&#8230; too greedy and grab unneeded data. Here is how to ask for less data, using <a href="http://www.cakephp.org/">CakePHP</a>.</p>
<p>Today, I listed only name and id fields of a Model, and to restrict the <code>findAll()</code> to those things:</p>
<p><code style="white-space:normal;">
<pre style="overflow:auto;white-space:normal;">
`$data = $this->Thing->findAll( aa("Thing.user_id", $user_id), a("id", "name"), null, null, null, -1 );`
</pre>
<p></code></p>
<p>Breaking down the call, we have:</p>
<p><code>aa()</code> is a CakePHP convenience function to make an associative array. Odd parameters are keys, even params values. The parameters here creates a WHERE clause that matches the current user&#8217;s Things.</p>
<p><code>a()</code> does the same for regular arrays. The params list the two fields we need returned.</p>
<p>A couple of null settings, and then the crucial <code>-1</code>, which turns off the Model associations completely.</p>
<p>Grab less data!</p>
<p><b>Update:</b> here is how to use <code>unbindModel</code>. First take a look at the Thing model definition, courtesy of Mladen Mihajlovic:</p>
<pre><code>
<?php
// Our Thing model
class Thing extends Model {

  var $hasMany = array(
    ‘AnotherThing’ => array(
      ‘className’ => ‘AnotherThing’
    ),
    ‘SomeOtherThing’ => array(
      ‘className’ => ‘SomeOtherThing’
    ),
    ‘YetAnotherThing’ => array(
      ‘className’ => ‘YetAnotherThing’
    )
  );
}
?>
</code></pre>
<p>Now, in our controller, before calling <code>findAll</code>, we remove two of the associated Models:</p>
<p><code><br />
$this->Thing->unbindModel( array( "hasMany" => array( "AnotherThing", "SomeOtherThing" ) ) );<br />
</code></p>
<p>[tags]cakephp[/tags]</p>
]]></content:encoded>
			<wfw:commentRss>http://ollehost.dk/blog/2006/09/21/cakephp-optimization-hint/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

