Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Changing the Order Sequence of CakePHP Behaviors
Posted on 02/25/2010 at 07:20 am by Kevin Wentworth
Viewed 13,303 times | 0 comments
For reasons too long to explain in this post, I have two behaviors that are run on the same pieces of data (a translate behavior and a settings import/export behavior). One behavior is attached to the model using var $actsAs, while the other behavior is attached dynamically, during runtime using $this->attachBehavior();. I needed the "hardcoded" behavior to run after the dynamically attached behavior. Here's what I did:
Changing the Order of CakePHP Behaviors via $actsAs
First, the simplest way to change the order of how behaviors are run (and the sequence that data will be manipulated) is to order the Behaviors in the appropriate way using the $actsAs variable. If you can't do it this way, then you need to dynamically change the order of attached behaviors.
Changing the Order of Attached Behaviors from the Controller
Because I couldn't change the order in actsAs, I was resigned to changing the order that the behaviors are attached. I started trying to manipulate the $this->ModelName->Behaviors object, but took a step back and realized an easy solution (the way CakePHP developers intended): detach the "hardcoded" behavior and re-attach it after the dyanmically attached behavior. Simple, and it works:
- $this->Form->Behaviors->detach('Avsettings'); //want to run after translate behaviors (depends on order in Behaviors)
- $this->Form->attachTranslate(); //attaches translate behavior dynamically/at runtime
- $this->Form->Behaviors->attach('Avsettings'); //now it will run after translate behaviors
The CakePHP manual has a great page that describes dynamically attaching behaviors as well as enabling and disabling behaviors.
Cheers,
-Kevin Wentworth
Tags for Changing the Order Sequence of CakePHP Behaviors
Cakephp | Web Programming | Tutorial | Usage | Behaviors | Example
Comments for this Posting
No comments. Be the first to post a reply.
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!