4

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_model.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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
miCRoSCoPiC_eaRthLinG
  • 2,910
  • 4
  • 40
  • 56

2 Answers2

5

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

evilbloodydemon
  • 530
  • 3
  • 6
  • Awesome dude :) Appreciate your effort. – miCRoSCoPiC_eaRthLinG Dec 23 '10 at 09:38
  • Hello, I tried your code in my project - but it seems that the records are getting hard deleted instead of the 'deleted' field being set to 0. I included your code as it's supposed to be, i.e. as a behavior and set the 'field' to point to the 'deleted' field in my table. Still it keeps hard-deleting when I call the delete() method from the controller. Am I missing out on something here? – miCRoSCoPiC_eaRthLinG Dec 27 '10 at 04:22
  • Never mind. A simple typo. I was using $actAs instead of $actsAs. – miCRoSCoPiC_eaRthLinG Dec 28 '10 at 04:05
3

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

deizel.
  • 11,042
  • 1
  • 39
  • 50