<?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>Yet another blog about PHP, HTML and CSS &#187; CakePHP</title>
	<atom:link href="http://blog.pepa.info/php-html-css/category/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pepa.info</link>
	<description>Petr 'PePa' Pavel</description>
	<lastBuildDate>Thu, 19 Aug 2010 18:17:58 +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>Baking with XAMPP on Windows</title>
		<link>http://blog.pepa.info/php-html-css/cakephp/baking-with-xampp-on-windows/</link>
		<comments>http://blog.pepa.info/php-html-css/cakephp/baking-with-xampp-on-windows/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 20:05:06 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=153</guid>
		<description><![CDATA[I&#8217;m not sure if it&#8217;s just my set-up but when I run cake.bat and say, try to bake a model I get &#160; Fatal error: Call to undefined function mysqli_connect() in C:\&#8230;\cake\libs\model\datasources\dbo\dbo_mysqli.php on line 73 &#160; The reason is that php.exe when run from a command line uses a different php.ini than PHP that&#8217;s run [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure if it&#8217;s just my set-up but when I run cake.bat and say, try to bake a model I get</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Fatal error: Call to undefined function mysqli_connect() in C:\&#8230;\cake\libs\model\datasources\dbo\dbo_mysqli.php on line 73
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The reason is that php.exe when run from a command line uses a different php.ini than PHP that&#8217;s run as an Apache module. To fix this edit cake.bat and add <code>-c"C:\xampp\apache\bin\php.ini"</code>. This is the path where XAMPP placed php.ini.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/cakephp/baking-with-xampp-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting rid of JOINs in updateAll() query</title>
		<link>http://blog.pepa.info/php-html-css/cakephp/getting-rid-of-joins-in-updateall-query/</link>
		<comments>http://blog.pepa.info/php-html-css/cakephp/getting-rid-of-joins-in-updateall-query/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 21:28:24 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=144</guid>
		<description><![CDATA[I guess I haven&#8217;t mastered the model relationships enough in order to appreciate them &#8211; and use correctly. On one hand they seem to be really cool and provide an easy access to linked database records while on the other hand, they just seem to stand in the way too often. Caring about database queries [...]]]></description>
			<content:encoded><![CDATA[<p>I guess I haven&#8217;t mastered the model relationships enough in order to appreciate them &#8211; and use correctly. On one hand they seem to be really cool and provide an easy access to linked database records while on the other hand, they just seem to stand in the way too often.<br />
<span id="more-144"></span><br />
Caring about database queries performance as I am I find myself unbinding models more often than I might be binding them. I guess I should not set default binding in the model but that&#8217;s just a natural place to put the logic. I&#8217;m trying to follow <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> as much as possible.</p>
<p>Anyway, here&#8217;s a piece of code that unbinds all belongsTo models in order to remove JOINs from an updateAll() query.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$content</span> = <span class="kw2">new</span> Content<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$content</span>-&gt;<span class="me1">unbindModel</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;belongsTo&#8217;</span> =&gt; <a href="http://www.php.net/array_keys"><span class="kw3">array_keys</span></a><span class="br0">&#40;</span><span class="re0">$content</span>-&gt;<span class="me1">belongsTo</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$content</span>-&gt;<span class="me1">updateAll</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;num_of_purchases&#8217;</span> =&gt; <span class="st0">&#8216;num_of_purchases+1&#8242;</span><span class="br0">&#41;</span>, <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;id&#8217;</span> =&gt; <span class="re0">$purchasedContentIds</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>(suggested by <a href="http://groups.google.com/group/cake-php/browse_thread/thread/f51ee496aafdff23">Serge Rodovnichenko</a>)</p>
<p>Setting $content-&gt;recursive = -1 doesn&#8217;t work. I debugged the call in CakePHP 1.2.5.</p>
<p><strong>Edit September 27th, 2009:</strong><br />
Let&#8217;s move this up to another level &#8211; let&#8217;s override the default updateAll() and add support for recursive = -1. Add this to your app_model.php</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp;* A workaround for CakePHP lack of support for recursive</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp;*/</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">function</span> updateAll<span class="br0">&#40;</span><span class="re0">$fields</span>, <span class="re0">$conditions</span> = <span class="kw2">true</span>, <span class="re0">$recursive</span> = <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$recursive</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re0">$recursive</span> = <span class="re0">$this</span>-&gt;<span class="me1">recursive</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$recursive</span> == <span class="nu0">-1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">unbindModel</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;belongsTo&#8217;</span> =&gt; <a href="http://www.php.net/array_keys"><span class="kw3">array_keys</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">belongsTo</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;hasOne&#8217;</span> =&gt; <a href="http://www.php.net/array_keys"><span class="kw3">array_keys</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">hasOne</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>, <span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> parent::<span class="me2">updateAll</span><span class="br0">&#40;</span><span class="re0">$fields</span>, <span class="re0">$conditions</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/cakephp/getting-rid-of-joins-in-updateall-query/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Switching layout using a url parameter</title>
		<link>http://blog.pepa.info/php-html-css/cakephp/switching-a-layout-using-a-url-parameter/</link>
		<comments>http://blog.pepa.info/php-html-css/cakephp/switching-a-layout-using-a-url-parameter/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 22:57:35 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=99</guid>
		<description><![CDATA[I needed to open a static page in a pop-up window. For static pages we have a handy controller Pages but I wanted to use a different layout for each page and do it in a flexible way. The magic of my solution is in three lines of code in AppController (app/app_controller.php). I simplify the [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to open a static page in a pop-up window. For static pages we have a handy controller Pages but I wanted to use a different layout for each page and do it in a flexible way.<br />
<span id="more-99"></span><br />
The magic of my solution is in three lines of code in AppController (app/app_controller.php). I simplify the code for the sake of clarity:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> AppController <span class="kw2">extends</span> Controller <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">function</span> beforeFilter<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">passedArgs</span><span class="br0">&#91;</span><span class="st0">&#8216;layout&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">layout</span> = <span class="re0">$this</span>-&gt;<span class="me1">passedArgs</span><span class="br0">&#91;</span><span class="st0">&#8216;layout&#8217;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I created a file /app/views/layouts/popup.ctp that contains only:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span> <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$content_for_layout</span>; <span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>And I call the static page as:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">http:<span class="co1">//test.com/pages/termsOfMembership/layout:popup</span></div>
</li>
</ol>
</div>
<p>Which is created using this call:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$html</span>-&gt;<span class="me1">link</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="st0">&#8216;Terms of Membership&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;Router::<span class="me2">url</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;controller&#8217;</span> =&gt; <span class="st0">&#8216;pages&#8217;</span>, <span class="st0">&#8216;action&#8217;</span> =&gt; <span class="st0">&#8216;termsOfMembership&#8217;</span>, <span class="st0">&#8216;layout&#8217;</span> =&gt; <span class="st0">&#8216;popup&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp;<span class="st0">&#8216;default&#8217;</span> =&gt; <span class="kw2">false</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="st0">&#8216;onclick&#8217;</span> =&gt; <span class="st0">&quot;window.open(this.href);&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#41;</span>,</div>
</li>
</ol>
</div>
<p>With this setting in my /app/config/routes.php:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Router::<span class="me2">connect</span><span class="br0">&#40;</span><span class="st0">&#8216;/pages/*&#8217;</span>, <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;controller&#8217;</span> =&gt; <span class="st0">&#8216;pages&#8217;</span>, <span class="st0">&#8216;action&#8217;</span> =&gt; <span class="st0">&#8216;display&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>That Router setting is redundant if you use urls that look like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">http:<span class="co1">//test.com/pages/display/termsOfMembership/layout:popup</span></div>
</li>
</ol>
</div>
<p>Then you also have to use a different call to the Router::url():</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;controller&#8217;</span> =&gt; <span class="st0">&#8216;pages&#8217;</span>, <span class="st0">&#8216;action&#8217;</span> =&gt; <span class="st0">&#8216;display&#8217;</span>, <span class="st0">&#8216;termsOfMembership&#8217;</span>, <span class="st0">&#8216;layout&#8217;</span> =&gt; <span class="st0">&#8216;popup&#8217;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/cakephp/switching-a-layout-using-a-url-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Populating a SELECT box with distinct values</title>
		<link>http://blog.pepa.info/php-html-css/cakephp/populating-a-select-box-with-distinct-values/</link>
		<comments>http://blog.pepa.info/php-html-css/cakephp/populating-a-select-box-with-distinct-values/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 22:13:24 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=90</guid>
		<description><![CDATA[Let&#8217;s say you log events into a database table Log. Attribute event holds event type and you want to filter the log listing just by that. Here&#8217;s what you probably try at first: &#160; $this-&#62;set&#40;&#8216;events&#8217;, &#160; $this-&#62;Log-&#62;find&#40;&#8216;list&#8217;, &#160; &#160; array&#40;&#8216;fields&#8217; =&#62; &#8216;DISTINCT Log.event&#8217;, &#8216;Log.event&#8217;&#41;&#41;&#41;; &#160; It&#8217;s not gonna work and you will have to use [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you log events into a database table Log. Attribute <i>event</i> holds event type and you want to filter the log listing just by that.</p>
<p>Here&#8217;s what you probably try at first:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$this</span>-&gt;<span class="me1">set</span><span class="br0">&#40;</span><span class="st0">&#8216;events&#8217;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$this</span>-&gt;<span class="me1">Log</span>-&gt;<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#8216;list&#8217;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;fields&#8217;</span> =&gt; <span class="st0">&#8216;DISTINCT Log.event&#8217;</span>, <span class="st0">&#8216;Log.event&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>It&#8217;s not gonna work and you will have to use find(&#8216;all&#8217;) and Set::combine() to get what you want:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$events</span> = <span class="re0">$this</span>-&gt;<span class="me1">Log</span>-&gt;<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#8216;all&#8217;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;fields&#8217;</span> =&gt; <span class="st0">&#8216;DISTINCT Log.event&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$this</span>-&gt;<span class="me1">set</span><span class="br0">&#40;</span><span class="st0">&#8216;events&#8217;</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; Set::<span class="me2">combine</span><span class="br0">&#40;</span><span class="re0">$events</span>, <span class="st0">&#8216;{n}.Log.event&#8217;</span>, <span class="st0">&#8216;{n}.Log.event&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/cakephp/populating-a-select-box-with-distinct-values/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>SQL functions or keywords in CakePHP conditions</title>
		<link>http://blog.pepa.info/php-html-css/cakephp/sql-functions-or-keywords-in-cakephp-conditions/</link>
		<comments>http://blog.pepa.info/php-html-css/cakephp/sql-functions-or-keywords-in-cakephp-conditions/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 13:21:09 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=84</guid>
		<description><![CDATA[For those who missed the information, starting from CakePHP 1.2 it is no longer possible to use the magic tag -! in your condition to prevent your SQL function or keyword to be enclosed in quotes. The recommended way now is to place it into the key or not make the array associative at all. [...]]]></description>
			<content:encoded><![CDATA[<p>For those who missed the information, <a title="changeset details" href="https://trac.cakephp.org/changeset/5139" target="_blank">starting from CakePHP 1.2</a> it is no longer possible to use the magic tag -! in your condition to prevent your SQL function or keyword to be enclosed in quotes.</p>
<p>The recommended way now is to place it into the key or not make the array associative at all. It is no longer possible to put it into the value part of the definition.</p>
<p>So instead of:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&#8216;DATE_ADD(Payment.modified, INTERVAL 2 DAY) &gt;&#8217;</span> =&gt; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&#8216;CURRENT_DATE&#8217;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>you have to use:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&#8216;DATE_ADD(Payment.modified, INTERVAL 2 DAY) &gt; CURRENT_DATE&#8217;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/cakephp/sql-functions-or-keywords-in-cakephp-conditions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What you can expect as $results in afterFind()</title>
		<link>http://blog.pepa.info/php-html-css/cakephp/what-you-can-expect-as-results-in-afterfind/</link>
		<comments>http://blog.pepa.info/php-html-css/cakephp/what-you-can-expect-as-results-in-afterfind/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 16:48:53 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=71</guid>
		<description><![CDATA[Function afterFind() can be called in various contexts with different types of $results. If you don&#8217;t handle them all properly the function will not work right (which you may not care in some cases) and you&#8217;ll end up with notice level errors. Here are situations I stumbled upon, let me know if you have found [...]]]></description>
			<content:encoded><![CDATA[<p>Function afterFind() can be called in various contexts with different types of $results. If you don&#8217;t handle them all properly the function will not work right (which you may not care in some cases) and you&#8217;ll end up with notice level errors.</p>
<p><span id="more-71"></span></p>
<p>Here are situations I stumbled upon, let me know if you have found some more:</p>
<ol>
<li>if your model contains a validation rule &#8216;isUnique&#8217; then afterFind() gets called from $model-&gt;invalidFields() with a result of SELECT COUNT():<br />
<strong> array(array(array(&#8216;count&#8217; =&gt; 1)))</strong></li>
<li>another results scheme is yielded by a model association (e.g. via belongsTo):<br />
<strong> array(&#8216;id&#8217; =&gt; 123, &#8216;first_name&#8217; =&gt; &#8216;Joe&#8217;&#8230;)</strong></li>
<li>and then there&#8217;s the regular $model-&gt;find() which gives<br />
<strong> array(<br />
0 =&gt; array(&#8216;ModelName&#8217; =&gt; array(&#8216;id&#8217; =&gt; 123, &#8230;)),<br />
1 =&gt; array(&#8216;ModelName&#8217; =&gt; array(&#8216;id&#8217; =&gt; 124, &#8230;))<br />
)</strong></li>
</ol>
<p>So what is the best way to work with this omnishaped variable?</p>
<p>First skip everything if there are no results:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$results</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re0">$results</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Then check for case 2 (assuming your primary key is &#8216;id&#8217;):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$results</span><span class="br0">&#91;</span><span class="st0">&#8216;id&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// do your thing</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>then for case 3:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">elseif</span> <span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$results</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="re0">$this</span>-&gt;<span class="me1">alias</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">&#8216;id&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// do your thing</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>and lastly you can check for the count result if you care:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">elseif</span> <span class="br0">&#40;</span><a href="http://www.php.net/is_array"><span class="kw3">is_array</span></a><span class="br0">&#40;</span><span class="re0">$results</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span> &amp;amp;&amp;amp; <a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$results</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">&#8216;count&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// do your thing</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/cakephp/what-you-can-expect-as-results-in-afterfind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ganymede / PHPEclipse &#8211; not a good choice for debugging CakePHP</title>
		<link>http://blog.pepa.info/php-html-css/tools/ganymede-phpeclipse-cakephp-debug/</link>
		<comments>http://blog.pepa.info/php-html-css/tools/ganymede-phpeclipse-cakephp-debug/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 19:57:40 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=62</guid>
		<description><![CDATA[After hours of trying I finally conclude that CakePHP cannot be debugged in Eclipse 3.4.1 (Ganymede) and PHPEclipse 1.2.1.200810130444PRD (XDebug). My webroot is set to app/webroot and Eclipse stops on a breakpoint only if it&#8217;s in app/webroot/index.php Even after it stops there it won&#8217;t stop on a next breakpoint that is set in my controller. [...]]]></description>
			<content:encoded><![CDATA[<p>After hours of trying I finally conclude that CakePHP cannot be debugged in Eclipse 3.4.1 (Ganymede) and PHPEclipse 1.2.1.200810130444PRD (XDebug).</p>
<p>My webroot is set to app/webroot and Eclipse stops on a breakpoint only if it&#8217;s in app/webroot/index.php<br />
Even after it stops there it won&#8217;t stop on a next breakpoint that is set in my controller.</p>
<p>The only working solution I found is downgrading to Eclipse 3.3.2 (Europa) and PDT 1.0.3.v20080603. Fortunately both can run side by side so in theory, I can write new code in Ganymede and debug in Europa but that sucks. Programming is a constant debugging.</p>
<p>I could try to switch from XDebug to DBG. Maybe I will next time I have a day that I can waste.</p>
<h2>Update:</h2>
<p>New PDT 2.0.0.v20081030-1802 works with Ganymede/XDebug just fine. It also has features I have been missing like Mark Occurrences &#8211; one more reason for switching. It doesn&#8217;t have support for chm help files though (PHPEclipse does) and is generally slower than PHPEclipse.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/tools/ganymede-phpeclipse-cakephp-debug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
