<?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>cakebaker &#187; behavior</title>
	<atom:link href="http://cakebaker.42dh.com/tags/behavior/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Mon, 19 Jul 2010 14:23:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Named finder methods</title>
		<link>http://cakebaker.42dh.com/2008/07/04/named-finder-methods/</link>
		<comments>http://cakebaker.42dh.com/2008/07/04/named-finder-methods/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 15:28:50 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[behavior]]></category>
		<category><![CDATA[cakephp]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=609</guid>
		<description><![CDATA[Recently, I stumbled upon a new Rails feature called named_scope. Inspired by this feature I wrote &#8212; just for fun &#8212; a small behavior which allows you to define named finder methods (download). Let&#8217;s say we want to define a named finder method to get the most recent posts. The code for it is quite [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I stumbled upon a new <a href="http://www.rubyonrails.org">Rails</a> feature called <a href="http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality">named_scope</a>.</p>
<p>Inspired by this feature I wrote &#8212; just for fun &#8212; a small behavior which allows you to define named finder methods (<a href="http://cakebaker.42dh.com/downloads/named-finder-behavior/">download</a>). </p>
<p>Let&#8217;s say we want to define a named finder method to get the most recent posts. The code for it is quite straightforward (you can use the same options as for the built-in find method):</p>
<pre>
// in the Post model
public $actsAs = array('NamedFinder' =&gt; array('recent' =&gt; array('order' =&gt; 'Post.created DESC', 'limit' =&gt; 10)));
</pre>
<p>With this definition in place, you can then use the named finder method in the following way:</p>
<pre>
// in your controller (e.g. PostsController)
$this-&gt;Post-&gt;findRecent();
</pre>
<p>You can also combine them with &#8220;And&#8221; if you defined multiple named finders:</p>
<pre>
$this-&gt;Post-&gt;findRecentAndPublished();
</pre>
<p>I&#8217;m not sure if this behavior is really useful in practice, as it is much less powerful than the named_scope feature of Rails, and you can accomplish (almost) the same with custom model methods or by defining a <a href="http://cakebaker.42dh.com/2008/03/23/defining-custom-find-types/">custom find type</a>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/07/04/named-finder-methods/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>New core behavior: Containable</title>
		<link>http://cakebaker.42dh.com/2008/05/18/new-core-behavior-containable/</link>
		<comments>http://cakebaker.42dh.com/2008/05/18/new-core-behavior-containable/#comments</comments>
		<pubDate>Sun, 18 May 2008 16:28:25 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[behavior]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[feature]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=604</guid>
		<description><![CDATA[With changeset 6918 a new behavior has been introduced to CakePHP: Containable. It&#8217;s a mix of the original Containable behavior by Felix Geisendörfer (aka the_undefined) and the Bindable behavior by Mariano Iglesias. To use the new behavior, you either have to add it to the $actsAs property of your model: class Post extends AppModel { [...]]]></description>
			<content:encoded><![CDATA[<p>With <a href="https://trac.cakephp.org/changeset/6918">changeset 6918</a> a new behavior has been introduced to CakePHP: Containable. It&#8217;s a mix of the original Containable behavior by <a href="http://debuggable.com">Felix Geisendörfer (aka the_undefined)</a> and the Bindable behavior by <a href="http://cricava.com/blogs/mariano.php">Mariano Iglesias</a>.</p>
<p>To use the new behavior, you either have to add it to the $actsAs property of your model:</p>
<pre>
class Post extends AppModel {
    var $actsAs = array('Containable');
}
</pre>
<p>or you can attach the behavior on the fly with:</p>
<pre>
$this-&gt;Post-&gt;Behaviors-&gt;attach('Containable');
</pre>
<p>Ok, now let&#8217;s have a look at some examples. For the examples I will use the three models Post, Comment, and Tag, with the associations: Post hasMany Comments, Post hasAndBelongsToMany Tags.</p>
<p>If we perform a simple find all statement, then we get back all posts plus all associated records, something like:</p>
<pre>
debug($this-&gt;Post-&gt;find('all'));

[0] =&gt; Array
        (
            [Post] =&gt; Array
                (
                    [id] =&gt; 1
                    [title] =&gt; First article
                    [content] =&gt; aaa
                    [created] =&gt; 2008-05-18 00:00:00
                )
            [Comment] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [id] =&gt; 1
                            [post_id] =&gt; 1
                            [author] =&gt; Daniel
                            [email] =&gt; dan@example.com
                            [website] =&gt; http://example.com
                            [comment] =&gt; First comment
                            [created] =&gt; 2008-05-18 00:00:00
                        )
                    [1] =&gt; Array
                        (
                            [id] =&gt; 2
                            [post_id] =&gt; 1
                            [author] =&gt; Sam
                            [email] =&gt; sam@example.net
                            [website] =&gt; http://example.net
                            [comment] =&gt; Second comment
                            [created] =&gt; 2008-05-18 00:00:00
                        )
                )
            [Tag] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [id] =&gt; 1
                            [name] =&gt; A
                        )
                    [1] =&gt; Array
                        (
                            [id] =&gt; 2
                            [name] =&gt; B
                        )
                )
        )
</pre>
<p>But often you don&#8217;t want to get all those data, and that&#8217;s when the Containable behavior comes to the rescue. </p>
<p>To get only the posts, you can do the following:</p>
<pre>
$this-&gt;Post-&gt;contain();
debug($this-&gt;Post-&gt;find('all'));

or

debug($this-&gt;Post-&gt;find('all', array('contain' =&gt; false)));

or without the Containable behavior

$this-&gt;Post-&gt;recursive = -1;
debug($this-&gt;Post-&gt;find('all'));
</pre>
<p>With the contain() method resp. the &#8220;contain&#8221; option we specify from which of the associated models we want to get data. If we want to get all posts plus the associated tags (without the comments), we would do it in the following way:</p>
<pre>
$this-&gt;Post-&gt;contain('Tag'); // we could also use an array
debug($this-&gt;Post-&gt;find('all'));

or

debug($this-&gt;Post-&gt;find('all', array('contain' =&gt; 'Tag'))); // we could also use an array

or without the Containable behavior

$this-&gt;Post-&gt;unbindModel(array('hasMany' =&gt; array('Comment')));
debug($this-&gt;Post-&gt;find('all'));
</pre>
<p>As you can see, the code using the Containable behavior is much cleaner than the code without it.</p>
<p>But that&#8217;s not all the Containable behavior can do for you. You can also filter the data of the associated models. If you are interested in the posts and the names of the comment authors, you could write:</p>
<pre>
$this-&gt;Post-&gt;contain('Comment.author');
debug($this-&gt;Post-&gt;find('all'));

or

debug($this-&gt;Post-&gt;find('all', array('contain' =&gt; 'Comment.author')));

[0] =&gt; Array
        (
            [Post] =&gt; Array
                (
                    [id] =&gt; 1
                    [title] =&gt; First article
                    [content] =&gt; aaa
                    [created] =&gt; 2008-05-18 00:00:00
                )
            [Comment] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [author] =&gt; Daniel
                            [post_id] =&gt; 1
                        )
                    [1] =&gt; Array
                        (
                            [author] =&gt; Sam
                            [post_id] =&gt; 1
                        )
                )
        )
</pre>
<p>As you can see, the comment arrays only contain the author plus the post_id (which is needed by Cake to map the results).</p>
<p>You can also filter the (comment) data by using a condition:</p>
<pre>
$this-&gt;Post-&gt;contain('Comment.author = "Daniel"');
debug($this-&gt;Post-&gt;find('all'));

or 

debug($this-&gt;Post-&gt;find('all', array('contain' =&gt; 'Comment.author = "Daniel"')));

[0] =&gt; Array
        (
            [Post] =&gt; Array
                (
                    [id] =&gt; 1
                    [title] =&gt; First article
                    [content] =&gt; aaa
                    [created] =&gt; 2008-05-18 00:00:00
                )
            [Comment] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [id] =&gt; 1
                            [post_id] =&gt; 1
                            [author] =&gt; Daniel
                            [email] =&gt; dan@example.com
                            [website] =&gt; http://example.com
                            [comment] =&gt; First comment
                            [created] =&gt; 2008-05-18 00:00:00
                        )
                )
        )
</pre>
<p>As you can see from these examples, the Containable behavior is very powerful, and I recommend to have a look at the tests.</p>
<p>Anyway, it&#8217;s very cool to have this behavior in the core :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/05/18/new-core-behavior-containable/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Attaching and detaching model behaviors on-the-fly</title>
		<link>http://cakebaker.42dh.com/2008/02/02/attaching-and-detaching-model-behaviors-on-the-fly/</link>
		<comments>http://cakebaker.42dh.com/2008/02/02/attaching-and-detaching-model-behaviors-on-the-fly/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 08:08:02 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[behavior]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[feature]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2008/02/02/attaching-and-detaching-model-behaviors-on-the-fly/</guid>
		<description><![CDATA[A feature recently added to CakePHP is the possibility to attach and detach model behaviors on-the-fly (see changeset 6415). Let&#8217;s have a look at this feature with a simple example. First we create a behavior which outputs a text before a find operation is performed: // app/models/behaviors/example.php class ExampleBehavior extends ModelBehavior { private $text = [...]]]></description>
			<content:encoded><![CDATA[<p>A feature recently added to CakePHP is the possibility to attach and detach model behaviors on-the-fly (see <a href="https://trac.cakephp.org/changeset/6415">changeset 6415</a>). Let&#8217;s have a look at this feature with a simple example. </p>
<p>First we create a behavior which outputs a text before a find operation is performed:</p>
<pre>
// app/models/behaviors/example.php
class ExampleBehavior extends ModelBehavior {
    private $text = 'this is an example text';

    function setUp(&#038;$model, $config) {
        if (isset($config['text'])) {
            $this-&gt;text = $config['text'];
        }
    }

    function beforeFind(&#038;$model, $query) {
        echo $this-&gt;text;

        return true;
    }
}
</pre>
<p>The usual way to use a behavior is to define it with the $actsAs variable in the model. For example, to use the ExampleBehavior with the Post model, we would define it in the following way:</p>
<pre>
// app/models/post.php
class Post extends AppModel {
    var $actsAs = array('Example' =&gt; array('text' =&gt; 'some text'));
}
</pre>
<p>Every time we execute a find method of the Post model, the string &#8220;some text&#8221; is echoed. </p>
<p><del datetime="2008-08-14T15:50:49+00:00">With the new methods Model::attach() and Model::detach()</del> With having a BehaviorCollection (with the methods attach() and detach()) as a property of the model more flexible scenarios are possible (even though I can&#8217;t think of any real use cases at the moment). If we want our Post model to act as Example only temporarily, we could write the following code:</p>
<pre>
// app/models/post.php
class Post extends AppModel {
}

// action in our controller
function index() {
    $this-&gt;Post-&gt;Behaviors-&gt;attach('Example', array('text' =&gt; 'hello world'));
    $this-&gt;set('all_posts', $this-&gt;Post-&gt;find('all'));
    $this-&gt;Post-&gt;Behaviors-&gt;detach('Example');
    $this-&gt;set('first_post', $this-&gt;Post-&gt;findById(1));
}
</pre>
<p>When we perform the find(&#8216;all&#8217;) statement, our Post model acts as Example, i.e. the specified text is echoed, whereas with the findById statement, it no longer acts as Example, and so there is no output. </p>
<p>Happy baking :)</p>
<p>Update (2008-08-14): The example now uses the methods of the BehaviorCollection object, which is a property of the model. Thanks to <a href="http://tumble.kabturek.info/">kabturek</a> for the hint!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/02/02/attaching-and-detaching-model-behaviors-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
