Soft Deletable Behavior and the Model::exists() gotcha

If you ever used the Soft Deletable Behavior, you will realise that Model::del() always returns a FALSE. This is expected after knowing that the behavior intercepts the delete request by saving the deleted flag and date then returning FALSE so the actual delete don’t happen.

So since I can’t rely on the returned value of Model::del(), this is what I thought will work until one fine day the application returns a failure when the record seems to be soft deleted correctly.


// some action of a controller
$this->Model->id = (int) $idPassed;
$this->Model->del();

if ($this->Model->exists()) {
    // say success!
} else {
    // say failure!
}

Alas, Model::exists() sets callbacks to FALSE in its find('count') call. So this means no beforeFind nor afterFind callbacks gets executed. This is understandable since Model uses exists() in many places where it needs to reflect true existence. So to get the expected results, we have to set callbacks to TRUE instead.


$this->exists(array('callbacks' => true));
Bookmark and Share
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
    blog comments powered by Disqus