<?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>damonky.co.uk &#187; cakePHP</title>
	<atom:link href="http://www.damonky.co.uk/tag/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.damonky.co.uk</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 04 Apr 2010 19:31:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Query conditions on related tables in CakePHP</title>
		<link>http://www.damonky.co.uk/php/query-conditions-on-related-tables-in-cakephp/</link>
		<comments>http://www.damonky.co.uk/php/query-conditions-on-related-tables-in-cakephp/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 14:36:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[recusive table conditions]]></category>
		<category><![CDATA[relationship model]]></category>
		<category><![CDATA[remoting]]></category>

		<guid isPermaLink="false">http://www.damonky.co.uk/2009/01/query-conditions-on-related-tables-in-cakephp/</guid>
		<description><![CDATA[Today I came across an interesting problem. When using the recursive features of CakePHP to return a results tree it isn&#8217;t possible to refine the results by a condition on the related table when it is a one to many relationship. Take the follow example:
I have 4 tables:
    User
    [...]]]></description>
			<content:encoded><![CDATA[<p>Today I came across an interesting problem. When using the recursive features of CakePHP to return a results tree it isn&#8217;t possible to refine the results by a condition on the related table when it is a one to many relationship. Take the follow example:</p>
<p>I have 4 tables:</p>
<blockquote><p><code lang="mysql">    User<br />
    -------------------<br />
    id<br />
    username<br />
    password<br />
    country_id</p>
<p>    Template<br />
    --------------------<br />
    id<br />
    template<br />
    code<br />
    user_id</p>
<p>    TemplateCountry<br />
    ---------------------<br />
    id<br />
    country_id<br />
    template_id</p>
<p>    Country<br />
    --------------------<br />
    id<br />
    country</p>
<p>    Templates have many TemplateCountries<br />
    Countries  have many TemplateCountries<br />
    Users have many Templates<br />
    TemplateCountries belongs to Templates<br />
    TemplateCountries belongs to Countries </code></p></blockquote>
<p>For the purpose of my experiment I am only retrieving the TemplateCountry entries directly related to the template, for a list in a Flex application I am building.</p>
<p>Now, I wish to return all of the templates and other associated records for a certain country / user. I expected to be able to use something along the lines of:</p>
<p><code lang="mysql">$this->Templates->find('all',array('conditions'=>array("Template.user_id" =>$user_id, "TemplateCountry.id" => $country_id))); </code></p>
<p>Unfortunately this does not work. This is because when the query is built by the cakePHP engine several queries are built to return the nested tree of results. The first query is simply:<br />
<code lang="mysql">SELECT * from template as Templates where Template.user_id = $user_id AND TemplateCountry.id = $country_id;</code><br />
This raises an error because the TemplateCountry table is not part of the initial query. My solution, all be it a cumborsome one, is to then take the result and apply a filter, much like is possible with arraycollections in Actionscript.<br />
<code lang="php">$result = $this->Templates->find('all',array('conditions'=>array("Template.user_id" =>$user_id));<br />
$result = $this->filterTemplates($result, $country_id);<br />
----------------------------------</p>
<p>function filterTemplates($tpl,$country_id) {</p>
<p>        //declare the return as an empty array<br />
        $out = array();</p>
<p>        //loop through the returns<br />
        foreach( $tpl as $item) {</p>
<p>                //check each country the template is associated to<br />
                foreach( $item['TemplateCountry'] as $country) {</p>
<p>                    //if it is the same country push to the new list<br />
                    if($country['country_id'] == $country_id) {</p>
<p>                        array_push($out, $item);</p>
<p>                    }</p>
<p>                }</p>
<p>        }<br />
        //$tpl = $out;<br />
        return $out;<br />
}</code></p>
<p>This prunes every record which doesn&#8217;t match my condition from the passed array and then replaces the array in the results tree. It&#8217;s not the perfect solution, but it does the job. If anyone has any other means of doing this I would love to hear from you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.damonky.co.uk/php/query-conditions-on-related-tables-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP: Displaying the correct label for lists and drop downs</title>
		<link>http://www.damonky.co.uk/general/cakephp-displaying-the-correct-label-for-lists-and-drop-downs/</link>
		<comments>http://www.damonky.co.uk/general/cakephp-displaying-the-correct-label-for-lists-and-drop-downs/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 16:33:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[$displayfield]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[find('list')]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://damonky.co.uk/2008/06/cakephp-displaying-the-correct-label-for-lists-and-drop-downs-2/</guid>
		<description><![CDATA[A little way into my project, after setting up CakePHP and baking my MVC&#8217;s i hit a stumbling block. When i used viewed the edit and add forms, the drop downs just showed Numbers, the Id of each related item to be precise.

As you can see this isn&#8217;t much help.
The data is pulled in using [...]]]></description>
			<content:encoded><![CDATA[<p>A little way into my project, after setting up CakePHP and baking my MVC&#8217;s i hit a stumbling block. When i used viewed the edit and add forms, the drop downs just showed Numbers, the Id of each related item to be precise.</p>
<p><img style="max-width: 800px;" src="http://localhost/wp-content/uploads/2008/06/cake-id-as-label.jpg" alt="" /></p>
<p>As you can see this isn&#8217;t much help.</p>
<p>The data is pulled in using the find(&#8217;list&#8217;) function. After trawling through the user groups I found that you can define which field is displayed when listed in the model ( which i probably should have seen in the manual, but it never occurred to me).</p>
<p>By adding the $displayField variable to a model and assigning the field name to it you can customise which fields is used:<br />
<em></em></p>
<p>[PHP] var $displayField = &#8216;Region&#8217;;[/PHP]</p>
<p><img style="max-width: 800px;" src="http://localhost/wp-content/uploads/2008/06/cake-id-as-label2.jpg" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.damonky.co.uk/general/cakephp-displaying-the-correct-label-for-lists-and-drop-downs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting back into cakePHP</title>
		<link>http://www.damonky.co.uk/uncategorized/getting-back-into-cakephp/</link>
		<comments>http://www.damonky.co.uk/uncategorized/getting-back-into-cakephp/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 12:47:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[1.2]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[Fat models]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[renderElement]]></category>
		<category><![CDATA[skinny controllers]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://damonky.co.uk/2008/06/getting-back-into-cakephp/</guid>
		<description><![CDATA[I have just started an internal project at work to create a portfolio management system. After several months of Joomla hacking it seems this is my chance to get back into serious development again.
I have gone for cakePHP for the project, as opposed to Codeigniter. Although I have mainly tended towards codeigniter in the past [...]]]></description>
			<content:encoded><![CDATA[<p>I have just started an internal project at work to create a portfolio management system. After several months of Joomla hacking it seems this is my chance to get back into serious development again.</p>
<p>I have gone for <a target="_blank" href="http://www.cakephp.org">cakePHP</a> for the project, as opposed to <a target="_blank" href="http://www.codeigniter.com">Codeigniter</a>. Although I have mainly tended towards codeigniter in the past for applications, It has been a while since i have really used cake i was drawn in by cakePHP&#8217;s features.</p>
<p>One feature i am finding particularly useful is the elements (using renderElement), as a lot of the forms use similar listings. Instead of using requestAction, I load the data into a standard variable (e.g: $campaigns, for campaigns) from either data already loaded or  direct from a custom function in the model. This removes the need to call a particular view from within the element and reduces the bloating of the controller as suggested by the article <a target="_blank" href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model">Skinny Controller, Fat Model</a>. </p>
<p>Jamis says:<br />
<blockquote>Be aggressive! Try to keep your controller actions and views as slim as<br />
possible. A one-line action is a thing of wonder, as is a template that<br />
is mostly <span class="caps">HTML</span>. It is also <em>much</em> more maintainable than a view that is full of assignment statements and chained method calls.</p></blockquote>
<p>I have found that this approach has indeed made my life a lot easier, It&#8217;s soo easy to get tangled up in controllers, and sometimes reapeating code when there is simply no need.</p>
<p>I haven&#8217;t tried to understand the paging yet, but based on a couple blogs I&#8217;ve read, it works the same as the find command, although I&#8217;m not sure how paging would work with several tables on the page &#8211; I guess I&#8217;ll find out in the next few days.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.damonky.co.uk/uncategorized/getting-back-into-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
