Monday, July 28, 2014

Re: Behavior method won't work

Okay, I think I got it. I've made a single function as you suggested.

Controller

    public function delete($id)
    {
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }
        
        $this->Attraction->id = $id;
        
        if ($this->Attraction->softDelete($id)) {
            $this->Session->setFlash(__('The attraction with id %s was successfully deleted.', h($id)), 'default', array(
                'class' => 'alert alert-success'
            ));
        }
        
        return $this->redirect(array(
            'action' => 'index'
        ));
    }

AppModel

 class AppModel extends Model {
    
    // Soft delete functionaliteit
    
    public function beforeDelete()
    {
        if ($this->hasField('deleted')) {
            $this->saveField('deleted', date('Y-m-d H:i:s'));
        }
        
        return false;
    }
    
    public function softDelete($id)
    {
        $this->delete($id);
        
        $record = $this->find('count', array(
            'conditions' => array(
                "{$this->alias}.id" => $id,
                "{$this->alias}.deleted !=" => null
            )
        ));
        if ($record > 0) {
            return true;
        }
    }
}

And that works fine for me now! Stephen, thank you for you time and thank you very much to support me on this one!
I've pasted my code here so others can use this functionality too! ;)

--
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: