Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP: Triggering Custom Event in Behaviour
    text
    copied!<p>Because of how my application is built, I need to create event handlers for <code>beforeSaveAssociated</code> and <code>afterSaveAssociated</code>. To allow for this, I've updated AppModel.php to contain the following relevant code:</p> <pre><code>public function saveAssociated(array $data = null, array $options = array()) { $this-&gt;after_save_options = NULL; $event = new CakeEvent('Model.beforeSaveAssociated', $this, array(&amp;$data, &amp;$options)); $this-&gt;after_save_options = NULL; $this-&gt;getEventManager()-&gt;dispatch($event); if (parent::saveAssociated($data, $options)) { if (is_array($this-&gt;after_save_options)) { $curData = $this-&gt;data; $this-&gt;data = $this-&gt;_tempData; $event = new CakeEvent('Model.afterSaveAssociated', $this, $this-&gt;after_save_options); $this-&gt;after_save_options = NULL; $this-&gt;getEventManager()-&gt;dispatch($event); $this-&gt;data = $curData; } if ($this-&gt;_tempData) { $this-&gt;_tempData = FALSE; } return TRUE; } return FALSE; } public function implementedEvents() { return array_merge(parent::implementedEvents(), array( 'Model.beforeSaveAssociated' =&gt; array( 'callable' =&gt; 'beforeSaveAssociated', 'passParams' =&gt; TRUE, ), 'Model.afterSaveAssociated' =&gt; array( 'callable' =&gt; 'afterSaveAssociated', 'passParams' =&gt; TRUE, ), )); } </code></pre> <p>Although this works fine for any <code>beforeSaveAssociated</code> defined within a model class, whenever I define it in a behaviour, it doesn't get triggered. If I update <code>saveAssociated</code> above to trigger <code>Model.beforeSave</code> (a built-in event), it does work, so as far as I can tell, it's not an issue with the behaviour not being properly attached.</p> <p>Any help is greatly appreciated,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload