开发者

CakePHP 1.3 alternative to SoftDeletable Behavior?

开发者 https://www.devze.com 2023-01-31 14:34 出处:网络
Does anyone know of an alternative to the SoftDeletable Behavior that is compatible with Cake 1.3.x? If there aren\'t any ready behaviors available, any suggestions on how I go about doing this in th

Does anyone know of an alternative to the SoftDeletable Behavior that is compatible with Cake 1.3.x?

If there aren't any ready behaviors available, any suggestions on how I go about doing this in the latest Cake?


Figured out a quick hack. First and foremost, if your table introduce a tinyint(1) unsigned field named deleted which defaults to 0.

In app/app_mo开发者_如何学编程del.php, add in the following function:

function softDelete( $id ) {
    if( $id && $this->hasField( 'deleted' ) ) {
        $this->id = $id;
        return $this->saveField( 'deleted', 1 );
    }

    return false;
}

and then from your controller's method (that performs the delete) call,

$this->Model->softDelete( $id );

Catch is, wherever you perform a find(), you need to specify the condition deleted != 1.

Still trying to figure out how to implement this in the same manner as the SoftDeletable behavior.


i've adapted mariano's behavior to 1.3. look here - https://github.com/evilbloodydemon/cakephp-softdeletable2


Also worth noting: there is a SoftDelete behavior bundled in CakeDC's Utils plugin.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号