On Thu, Mar 17, 2011 at 12:57 AM, Krissy Masters <[email protected]> wrote: > I am thinking of attempting my first behavior but could not find much help > with my questions I need answered in the cookbook or online so here goes. > > Example: > Model Post > var $actsAs = array( > 'Orderable' => array( > 'identified' => 'user_id')); > > > I need to hook into a delete callback so model Post delete record then the > Orderable behavior kicks in and does something but do I need to manually > call the behavior in model after delete? > > And how do I pass data to the behavior? User deletes a Post belong to > himself how do I access the User or Post data or pass it to the behavior? > > We User.id 159 deletes Post.id 258 how can I use 159 and 258 in the > behavior? > > If you know good link to point out that would be great.
http://api.cakephp.org/ Dive into the Model class with me: http://api.cakephp.org/view_source/model/#line-1808 1814 if ($this->beforeDelete($cascade)) { 1815 $filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array( 1816 'break' => true, 'breakOn' => false 1817 )); What this code is doing is first running any beforeDelete() in the model. Note that if your model does contain such a method it must return true for the delete to continue on its way. Next, the beforeDelete() callbacks of any behaviors attached to the model are run. Finally, after the record is removed from the DB, the model's afterDelete(), if any, is called, followed by any afterDelete() in the behaviors. So that's the order of things. Notice that the model itself is passed as the first param to trigger(). So, in a behavior, these methods' signatures should look like: function beforeDelete(&$Model) function afterDelete(&$Model) At the very top of Model::delete() the id that's passed as the first param is set to $this->id. Then, the model is passed to the callback in the behavior. This allows you to fetch the record from the DB. By judiciously settingsome class vars (the behavior's, I mean) you can then refer to them in your afterDelete callback. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php
