<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Attach &amp; detach behaviors at run-time in CakePHP Models</title>
	<atom:link href="http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=attach-detach-behaviors-at-run-time-in-cakephp-models</link>
	<description>sharing technology, ideas, insights!</description>
	<lastBuildDate>Thu, 02 Feb 2012 08:48:59 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Everton Yoshitani</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-5601</link>
		<dc:creator>Everton Yoshitani</dc:creator>
		<pubDate>Thu, 13 Jan 2011 16:47:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-5601</guid>
		<description>Just an update for CakePHP 1.3.x we can use BehaviorCollection

http://api13.cakephp.org/class/behavior-collection</description>
		<content:encoded><![CDATA[<p>Just an update for CakePHP 1.3.x we can use BehaviorCollection</p>
<p><a href="http://api13.cakephp.org/class/behavior-collection" rel="nofollow">http://api13.cakephp.org/class/behavior-collection</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iivs</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-2662</link>
		<dc:creator>iivs</dc:creator>
		<pubDate>Sun, 01 Mar 2009 11:07:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-2662</guid>
		<description>I found another solution. For example I have a model with a variable &lt;code&gt;var $actsAs   = array(&#039;i18n&#039; =&gt; array(&#039;fields&#039;=&gt;array(&#039;name&#039;)) );&lt;/code&gt;
I can detach that beavior any time and attach back again. I&#039;ve written 4 functions to do that. 1) private function to merge behavior array
&lt;code&gt;function __behaviors($behaviors = null)
	{
		if ($behaviors)
		{
			foreach ($behaviors as $index =&gt; $behavior) // Loop through method arguments
			{
				if (is_array($behavior)) // If method agrument is an array
				{
					if (count($behavior) &gt; 0) // If method agrument contains more than one element then merge it with method arguments
					{
						$behaviors = array_merge($behaviors, $behavior);
					}
					unset($behaviors[$index]); // Unset method argument from method arguments
				}
			}
			
			return $behaviors;
		}
	}&lt;/code&gt;
2) private function checks if behavior is disabled or not. if it is disabled, return array index else return false.
&lt;code&gt;function __is_disabled($behavior)
	{
		$disabled = false;
		if (isset($this-&gt;Behaviors-&gt;_disabled) and count($this-&gt;Behaviors-&gt;_disabled) &gt; 0)
		{
			foreach ($this-&gt;Behaviors-&gt;_disabled as $key =&gt; $val)
			{
				if ($val == $behavior)
				{
					$disabled = $key;
					break;
				}
			}
		}
		
		return $disabled;
	}&lt;/code&gt;
3) adds behaviors in $this-&gt;Model-&gt;Behaviors-&gt;_disabled array
function dontActAs()
	{
		$behaviors = $this-&gt;__behaviors(func_get_args());
		
		if ($behaviors and count($behaviors) &gt; 0)
		{
			foreach ($behaviors as $behavior)
			{
				if ($this-&gt;__is_disabled($behavior) == false)
					$this-&gt;Behaviors-&gt;_disabled[] = $behavior; //add new one
			}
		
			$this-&gt;schema(true); //rebuild model schema
		}
		
	}

4) removes from $this-&gt;Model-&gt;Behaviors-&gt;_disabled array
function nowActsAs()
	{
		$behaviors = $this-&gt;__behaviors(func_get_args());
		
		if ($behaviors and count($behaviors) &gt; 0)
		{
			foreach ($behaviors as $behavior)
			{
				$key = $this-&gt;__is_disabled($behavior);
				unset($this-&gt;Behaviors-&gt;_disabled[$key]);
			}
			
			$this-&gt;schema(true); //rebuild model schema
		}
	}

I also found some functions in cake/libs/model/behavior.php &quot;detach&quot;, &quot;disable&quot; &quot;enable&quot; etc. not sure how they work. I&#039;m still new to this cakephp thing.</description>
		<content:encoded><![CDATA[<p>I found another solution. For example I have a model with a variable <code>var $actsAs   = array('i18n' =&gt; array('fields'=&gt;array('name')) );</code><br />
I can detach that beavior any time and attach back again. I&#8217;ve written 4 functions to do that. 1) private function to merge behavior array<br />
<code>function __behaviors($behaviors = null)<br />
	{<br />
		if ($behaviors)<br />
		{<br />
			foreach ($behaviors as $index =&gt; $behavior) // Loop through method arguments<br />
			{<br />
				if (is_array($behavior)) // If method agrument is an array<br />
				{<br />
					if (count($behavior) &gt; 0) // If method agrument contains more than one element then merge it with method arguments<br />
					{<br />
						$behaviors = array_merge($behaviors, $behavior);<br />
					}<br />
					unset($behaviors[$index]); // Unset method argument from method arguments<br />
				}<br />
			}</p>
<p>			return $behaviors;<br />
		}<br />
	}</code><br />
2) private function checks if behavior is disabled or not. if it is disabled, return array index else return false.<br />
<code>function __is_disabled($behavior)<br />
	{<br />
		$disabled = false;<br />
		if (isset($this-&gt;Behaviors-&gt;_disabled) and count($this-&gt;Behaviors-&gt;_disabled) &gt; 0)<br />
		{<br />
			foreach ($this-&gt;Behaviors-&gt;_disabled as $key =&gt; $val)<br />
			{<br />
				if ($val == $behavior)<br />
				{<br />
					$disabled = $key;<br />
					break;<br />
				}<br />
			}<br />
		}</p>
<p>		return $disabled;<br />
	}</code><br />
3) adds behaviors in $this-&gt;Model-&gt;Behaviors-&gt;_disabled array<br />
function dontActAs()<br />
	{<br />
		$behaviors = $this-&gt;__behaviors(func_get_args());</p>
<p>		if ($behaviors and count($behaviors) &gt; 0)<br />
		{<br />
			foreach ($behaviors as $behavior)<br />
			{<br />
				if ($this-&gt;__is_disabled($behavior) == false)<br />
					$this-&gt;Behaviors-&gt;_disabled[] = $behavior; //add new one<br />
			}</p>
<p>			$this-&gt;schema(true); //rebuild model schema<br />
		}</p>
<p>	}</p>
<p>4) removes from $this-&gt;Model-&gt;Behaviors-&gt;_disabled array<br />
function nowActsAs()<br />
	{<br />
		$behaviors = $this-&gt;__behaviors(func_get_args());</p>
<p>		if ($behaviors and count($behaviors) &gt; 0)<br />
		{<br />
			foreach ($behaviors as $behavior)<br />
			{<br />
				$key = $this-&gt;__is_disabled($behavior);<br />
				unset($this-&gt;Behaviors-&gt;_disabled[$key]);<br />
			}</p>
<p>			$this-&gt;schema(true); //rebuild model schema<br />
		}<br />
	}</p>
<p>I also found some functions in cake/libs/model/behavior.php &#8220;detach&#8221;, &#8220;disable&#8221; &#8220;enable&#8221; etc. not sure how they work. I&#8217;m still new to this cakephp thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tarique Sani</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-2661</link>
		<dc:creator>Tarique Sani</dc:creator>
		<pubDate>Sun, 01 Mar 2009 03:39:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-2661</guid>
		<description>@iivs When the post was written there was a loadBehavior() thanks for the updated code</description>
		<content:encoded><![CDATA[<p>@iivs When the post was written there was a loadBehavior() thanks for the updated code</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iivs</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-2660</link>
		<dc:creator>iivs</dc:creator>
		<pubDate>Sat, 28 Feb 2009 23:28:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-2660</guid>
		<description>Oh, I found it. &lt;code&gt;$this-&gt;schema(true)&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Oh, I found it. <code>$this-&gt;schema(true)</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iivs</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-2659</link>
		<dc:creator>iivs</dc:creator>
		<pubDate>Sat, 28 Feb 2009 23:06:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-2659</guid>
		<description>Great code. Thanks! :) But there are several problems.
1) there is no loadBehavior in version 1.2 so I had to change ir to &lt;code&gt;App::import(&#039;Behavior&#039;, $behavior);&lt;/code&gt;
2) It doesn&#039;t work at all. To remove all (hopefully) behaviours you have to loop through attachments like this &lt;code&gt;foreach ($behaviors as $behavior) // Loop through passed behaviors
		{
			unset($this-&gt;actsAs);
			foreach ($this-&gt;Behaviors-&gt;_attached as $key =&gt; $attached)
			{
				if ($attached == $behavior)
					unset($this-&gt;Behaviors-&gt;_attached[$key]);
			}
			unset($this-&gt;Behaviors-&gt;$behavior);
			unset($this-&gt;locale);
		}&lt;/code&gt;
3) cakephp caches queries. You can&#039;t attach and detach behaviours in the next line. I&#039;m now trying to figure out how to rebuild model shema.</description>
		<content:encoded><![CDATA[<p>Great code. Thanks! <img src='http://www.sanisoft.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  But there are several problems.<br />
1) there is no loadBehavior in version 1.2 so I had to change ir to <code>App::import('Behavior', $behavior);</code><br />
2) It doesn&#8217;t work at all. To remove all (hopefully) behaviours you have to loop through attachments like this <code>foreach ($behaviors as $behavior) // Loop through passed behaviors<br />
		{<br />
			unset($this-&gt;actsAs);<br />
			foreach ($this-&gt;Behaviors-&gt;_attached as $key =&gt; $attached)<br />
			{<br />
				if ($attached == $behavior)<br />
					unset($this-&gt;Behaviors-&gt;_attached[$key]);<br />
			}<br />
			unset($this-&gt;Behaviors-&gt;$behavior);<br />
			unset($this-&gt;locale);<br />
		}</code><br />
3) cakephp caches queries. You can&#8217;t attach and detach behaviours in the next line. I&#8217;m now trying to figure out how to rebuild model shema.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thierry</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-1974</link>
		<dc:creator>Thierry</dc:creator>
		<pubDate>Wed, 23 Jan 2008 14:45:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-1974</guid>
		<description>Interesting...
I had checked cake 3 years ago, but didn&#039;t used it.

I&#039;m more of a &quot;self doer&quot; than to rely on framework, but the idea of relying to behaviors attached to elements surely looks nice.

I&#039;ll try to put some time on cake in the next week, I could be agreeably surprised.

Thanks for your article.</description>
		<content:encoded><![CDATA[<p>Interesting&#8230;<br />
I had checked cake 3 years ago, but didn&#8217;t used it.</p>
<p>I&#8217;m more of a &#8220;self doer&#8221; than to rely on framework, but the idea of relying to behaviors attached to elements surely looks nice.</p>
<p>I&#8217;ll try to put some time on cake in the next week, I could be agreeably surprised.</p>
<p>Thanks for your article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tarique Sani</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-1888</link>
		<dc:creator>Tarique Sani</dc:creator>
		<pubDate>Tue, 13 Nov 2007 03:57:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-1888</guid>
		<description>Hey! somebody actually used it :) thanks for the comment</description>
		<content:encoded><![CDATA[<p>Hey! somebody actually used it <img src='http://www.sanisoft.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  thanks for the comment</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zoltan</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-1887</link>
		<dc:creator>Zoltan</dc:creator>
		<pubDate>Tue, 13 Nov 2007 02:55:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-1887</guid>
		<description>Just used this on a project where I&#039;m occasionally updating fields , but don&#039;t want the regular behaviours to fire - thanks a lot.</description>
		<content:encoded><![CDATA[<p>Just used this on a project where I&#8217;m occasionally updating fields , but don&#8217;t want the regular behaviours to fire &#8211; thanks a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: developercast.com &#187; Sanisoft.com: Attach &#38; detach behaviors at run-time in CakePHP Models</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-30</link>
		<dc:creator>developercast.com &#187; Sanisoft.com: Attach &#38; detach behaviors at run-time in CakePHP Models</dc:creator>
		<pubDate>Tue, 26 Jun 2007 19:11:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-30</guid>
		<description>[...] guys over on Sanisoft.com dropped us a line today to let us know about a new entry on their blog showing how to implement attach and detach behaviors in CakePHP models.   Behaviors are one of the [...]</description>
		<content:encoded><![CDATA[<p>[...] guys over on Sanisoft.com dropped us a line today to let us know about a new entry on their blog showing how to implement attach and detach behaviors in CakePHP models.   Behaviors are one of the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PHPDeveloper.org</title>
		<link>http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/comment-page-1/#comment-29</link>
		<dc:creator>PHPDeveloper.org</dc:creator>
		<pubDate>Tue, 26 Jun 2007 13:45:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.sanisoft.com/blog/2007/06/26/attach-detach-behaviors-at-run-time-in-cakephp-models/#comment-29</guid>
		<description>&lt;strong&gt;Sanisoft.com:  Attach &amp; detach behaviors at run-time in CakePHP Models...&lt;/strong&gt;

...</description>
		<content:encoded><![CDATA[<p><strong>Sanisoft.com:  Attach &#38; detach behaviors at run-time in CakePHP Models&#8230;</strong></p>
<p>&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

