<?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>Derilicious&#187; Posts tagged with Model &#8211; Page  &#8211; Derilicious</title> <atom:link href="http://derickng.com/posts/tag/model/feed" rel="self" type="application/rss+xml" /><link>http://derickng.com</link> <description>A blog on web development technicalities by Derick Ng</description> <lastBuildDate>Sun, 20 Dec 2009 12:18:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Working with Associations using CakePHP</title><link>http://derickng.com/posts/56-working-with-associations-using-cakephp</link> <comments>http://derickng.com/posts/56-working-with-associations-using-cakephp#comments</comments> <pubDate>Sun, 18 Jan 2009 14:14:34 +0000</pubDate> <dc:creator>Derick Ng</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[Associations]]></category> <category><![CDATA[CakePHP]]></category> <category><![CDATA[ContainableBehavior]]></category> <category><![CDATA[Model]]></category><guid
isPermaLink="false">http://derick.lyniq.com/?p=56</guid> <description><![CDATA[I was asked by Niraja Mulye of Packt Publishing if I would be interested in publishing an article on my blog from the book, CakePHP Application Development. Since it seems relevant to any starting out with CakePHP, I guess why not.
Instead of copying and pasting what was sent, I found two articles which seemed like [...]]]></description> <content:encoded><![CDATA[<p>I was asked by Niraja Mulye of <a
href="http://www.packtpub.com">Packt Publishing</a> if I would be interested in publishing an article on my blog from the book, <a
href="http://www.packtpub.com/cakephp-application-development">CakePHP Application Development</a>. Since it seems relevant to any starting out with CakePHP, I guess why not.</p><p>Instead of copying and pasting what was sent, I found two articles which seemed like the original source. Being properly formatted, I guess it will be more appropriate for me to point them to you. The first article titled, <a
href="http://www.packtpub.com/article/working-with-simple-associations-using-cakephp">Working with Simple Associations using CakePHP</a> talks about the basic belongsTo, hasOne and hasMany assocations while the second article titled, <a
href="http://www.packtpub.com/article/working-with-complex-associations-using-cakephp">Working with Complex Associations using CakePHP</a> tackles the hasAndBelongsToMany association where most beginners have difficulty with.</p><p>The articles will be useful for anyone starting out but unfortunately, CakePHP had gone through lots of changes and the book (published on July 2008) is based on an older version of CakePHP. So now there are actually better ways of dealing with associations using the <a
href="http://book.cakephp.org/view/474/Containable">Containable Behavior</a>. The Containable Behavior binds and unbinds model on the fly, automatically setting the recursive level to suit. This means you no longer have to bother about setting the recursive level as long as you &#8220;contain&#8221; your <code>find()</code> queries. I am mentioning this because I have to warn against the setting of recursive level directly.</p><pre class="brush: php">

// All models have recursive level set to 1 by default
$this-&gt;Author-&gt;recursive = 2;
$this-&gt;Author-&gt;id = 1001;
$this-&gt;Author-&gt;find(&#039;first&#039;); // Return results based on recursive level of 2
// some
// codes
// in
// between
$this-&gt;Author-&gt;find(&#039;all&#039;); // Return results based on recursive level of 2 (as set previously) and not 1!
</pre><p>This causes CakePHP to retrieve more data than it should when it is unintended by you. My suggestion is to have all models default to recursive level -1 by setting it in the AppModel and let &#8220;contain&#8221; handle the rest.</p><pre class="brush: php">

$this-&gt;Author-&gt;id = 1001;
$this-&gt;Author-&gt;find(&#039;first&#039;, array(&#039;contain&#039; =&gt; &#039;Book&#039;)); // Returns author 1001 and all books authored by him/her
$this-&gt;Author-&gt;find(&#039;all&#039;); // Returns all authors only
</pre><p>How nice!</p> ]]></content:encoded> <wfw:commentRss>http://derickng.com/posts/56-working-with-associations-using-cakephp/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Soft Deletable Behavior and the Model::exists() gotcha</title><link>http://derickng.com/posts/31-soft-deletable-behavior-and-the-modelexists-gotcha</link> <comments>http://derickng.com/posts/31-soft-deletable-behavior-and-the-modelexists-gotcha#comments</comments> <pubDate>Sat, 15 Nov 2008 03:30:00 +0000</pubDate> <dc:creator>Derick Ng</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Model]]></category> <category><![CDATA[SoftDeletableBehavior]]></category><guid
isPermaLink="false">http://derick.lyniq.com/?p=31</guid> <description><![CDATA[If you ever used the Soft Deletable Behavior, you will realise that Model::del() always returns a FALSE. This is expected after knowing that the behavior intercepts the delete request by saving the deleted flag and date then returning FALSE so the actual delete don&#8217;t happen.
So since I can&#8217;t rely on the returned value of Model::del(), [...]]]></description> <content:encoded><![CDATA[<p>If you ever used the Soft Deletable Behavior, you will realise that <code>Model::del()</code> always returns a <code>FALSE</code>. This is expected after knowing that the behavior intercepts the delete request by saving the deleted flag and date then returning <code>FALSE </code>so the actual delete don&#8217;t happen.</p><p>So since I can&#8217;t rely on the returned value of <code>Model::del()</code>, this is what I thought will work until one fine day the application returns a failure when the record seems to be soft deleted correctly.</p><pre class="brush: php">

// some action of a controller
$this-&gt;Model-&gt;id = (int) $idPassed;
$this-&gt;Model-&gt;del();

if ($this-&gt;Model-&gt;exists()) {
    // say success!
} else {
    // say failure!
}
</pre><p>Alas, <code>Model::exists()</code> sets callbacks to <code>FALSE</code> in its <code>find('count')</code> call. So this means no <code>beforeFind</code> nor <code>afterFind</code> callbacks gets executed. This is understandable since Model uses <code>exists()</code> in many places where it needs to reflect true existence. So to get the expected results, we have to set callbacks to <code>TRUE</code> instead.</p><pre class="brush: php">

$this-&gt;exists(array(&#039;callbacks&#039; =&gt; true));
</pre>]]></content:encoded> <wfw:commentRss>http://derickng.com/posts/31-soft-deletable-behavior-and-the-modelexists-gotcha/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 7/16 queries in 0.004 seconds using disk

Served from: derickng.com @ 2012-02-05 04:00:43 -->
