Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When using callbacks, you can refer to the <a href="http://api.cakephp.org/classes" rel="nofollow noreferrer">API</a> for the class you are extending to check the parameters it accepts. Your implementation should accept, at minimum, the same parameters as the methods you are overriding.</p> <p>For example, <a href="http://api.cakephp.org/class/model#method-ModelbeforeDelete" rel="nofollow noreferrer"><code>Model::beforeDelete</code></a> is implemented like so:</p> <pre><code>/** * Called before every deletion operation. * * @param boolean $cascade If true records that depend on this record will also be deleted * @return boolean True if the operation should continue, false if it should abort * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforedelete */ public function beforeDelete($cascade = true) { return true; } </code></pre> <p>And also, <a href="http://api.cakephp.org/class/model-behavior#method-ModelBehaviorbeforeDelete" rel="nofollow noreferrer"><code>ModelBehavior::beforeDelete</code></a> is implemented like this (ie. when making a behavior):</p> <pre><code>/** * Before delete is called before any delete occurs on the attached model, but after the model's * beforeDelete is called. Returning false from a beforeDelete will abort the delete. * * @param Model $model Model using this behavior * @param boolean $cascade If true records that depend on this record will also be deleted * @return mixed False if the operation should abort. Any other result will continue. */ public function beforeDelete(Model $model, $cascade = true) { return true; } </code></pre> <p>Next, it is useful to know that when saving to a model and passing in the controller's data (ie. <code>$this-&gt;data</code> in the controller) that data is <a href="http://api.cakephp.org/class/model#method-Modelset" rel="nofollow noreferrer">set to the model</a> (ie. <code>$this-&gt;data</code> in the model). [This happens during <code>Model::save()</code>, currently on <a href="https://github.com/cakephp/cakephp/blob/c159854a172bf25cd4fd95765c7ea0d3305892e2/lib/Cake/Model/Model.php#L1623" rel="nofollow noreferrer">line 1225</a>.]</p> <p>In the case of the first example you can access the model using <code>$this</code> and in the second example you can access the model using <code>$model</code> (as <code>$this</code> would be the behavior in that context). So to get at the data, you want to use <code>$this-&gt;data</code> or <code>$model-&gt;data</code>. You can also access that model's related models using chaining (ie. <code>$this-&gt;RelatedModel</code> or <code>$model-&gt;RelatedModel</code>).</p> <p>As the docblock comments state, <code>$cascade</code> should let you know if this is a cascading delete that is happening (<code>true</code> by default) in case your code needs to take different actions when this is or isn't the case; and your implementation of the method should return false if you want to abort the save operation (otherwise, return true when you are done).</p> <p>There is a <a href="https://github.com/davidpersson/media" rel="nofollow noreferrer">Media plugin</a> for CakePHP which implements <a href="https://github.com/davidpersson/media/blob/a8db789e0ec767ee4f4ebf7af1a8c8df12768893/models/behaviors/coupler.php#L155-182" rel="nofollow noreferrer">this exact functionality</a> that can be used as a reference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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