Saturday, July 26, 2014

Re: Behavior method won't work

If you want to do this with a behavior then you should look at the beforeDelete callback, you can update your field within this callback and then return false to automatically abort the delete (as per the book).

http://book.cakephp.org/2.0/en/models/behaviors.html#ModelBehavior::beforeDelete

You could also just write something like this in your AppModel to make it apply to all models, my applications use $this->hasField('deleted') to see if soft delete is supported with the model.



On 26 July 2014 09:22, Sam Clauw <info@bellewaerdefun.be> wrote:
I'll try to write my very first behavior that does a soft delete instead of a hard delete. I've expanded my model with

public $actsAs = array('SoftDelete');

and I've created the behavior "SoftDeleteBehavior.php" in App/Model/Behavior:

 
<?php
App::uses('ModelBehavior', 'Model');
class SoftDeleteBehavior extends ModelBehavior
{
    function setup(Model $Model, $settings = array())
    {
        
    }
    
    function delete(Model $Model, $id = null)
    {
        $Model->id = $id;
        
        if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) {
            return true;
        }
        return false;
    }
}

However, when I try to delete something, the delete() method from my behavior doesn't overwrite the normal one. What could cause this issue?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: