Using Class Phalcon\Mvc\Model\Behavior\SoftDelete

How is the Class Phalcon\Mvc\Model\Behavior\SoftDelete used? I have several models I would like to soft delete and this seems to be made especially for that.

http://docs.phalconphp.com/en/latest/api/Phalcon%5Mvc%5Model%5Behavior%5SoftDelete.html

Couldn't find much info on how to use it though :\

edited Mar '14

Hi @norboost

here is a small example on how you could use soft delete.

In your DB table create a field >>deleted<<

In your model:

use \Phalcon\Mvc\Model\Behavior\SoftDelete,


public function initialize()
    {
        $this->addBehavior(new SoftDelete([
            'field' => 'deleted',
            'value' => '1'
        ]));
    }

and in your controller you could query the DB like:

public function indexAction()
{
    $this->view->setVars(array(
    'all' => Tasks::find(array(
        'deleted is NULL'
        ));
    ));
}

hope it helps, and sorry for my bad english. Dont have much time now to rewrite and correct it :)



10.2k

I also have a question about SoftDelete.

The code I have seems to add the value I've specified, but the find() function does not leave out the deleted records. Below I have pasted my model. I do not want to return any records that have a deleted not NULL. On the find, my Vendor model has a relationship defined and it is work as expected.

<?php

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Behavior\SoftDelete;

class Establishment extends Model {

    public $id;
    public $vendor_id;
    public $name;
    public $address1;
    public $address2;
    public $city;
    public $state_id;
    public $zip;
    public $country_id;
    public $description;
    public $lat;
    public $lng;
    public $created;
    public $updated;
    public $deleted;

    public function initialize() {
        $this->setSource('establishments');

        $this->addBehavior(new SoftDelete([
            'field' => 'deleted',
            'value' => date('Y-m-d H:i:s')
        ]));

        $this->hasOne('vendor_id','Vendor','id');
        $this->hasMany('id','Promotion','establishment_id');
        $this->hasOne('state_id','State','id');
        $this->hasOne('country_id','Country','id');
    }

    public function validation() {
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }

    /*
    public function beforeDelete() {
        $this->deleted = date('Y-m-d H:i:s');
    }
     */

    public function beforeValidationOnCreate() {
        $this->created = date('Y-m-d H:i:s');
        $this->updated = date('Y-m-d H:i:s');
    }

    public function beforeValidationOnUpdate() {
        $this->updated = date('Y-m-d H:i:s');
    }

    public function deleteEstablishment($request) {
        $user = $this->getDi()->getShared('session')->get('auth');
        $vendor = Vendor::findFirstByUserId($user->id);
        //make sure logged in vendor account matches the value passed by the UI
        if ($vendor->id == $request->vendor_id) {
            //make sure the establishment's vendor ID matches the currently logged in vendor
            if ($this->vendor_id == $vendor->id) {
                // Todo: delete current or upcoming promotions
                // Todo: refund any points associated w/ those promotions
                return $this->delete();
            } else {
                $this->appendMessage(new Message("Establishment not found."));
                return false;
            }
        } else {
            $this->appendMessage(new Message("Vendor not found."));
            return false;
        }
    }

    public function listEstablishments() {
        $user = $this->getDi()->getShared('session')->get('auth');
        $vendor = Vendor::findFirstByUserId($user->id);
        $establishments = $vendor->getEstablishment();
        $arr = [];
        foreach($establishments as $establishment) {
            $establishment->state = $establishment->getState();
            $establishment->country = $establishment->getCountry();
            $arr[] = $establishment;
        }
        return $arr;
    }

    public function saveEstablishment($request,$vendor_id) {
        //Vendor can't edit an establishment that isn't theirs.
        if (!empty($this->id) && $this->vendor_id != $vendor_id) {
            $this->appendMessage(new Message("Establishment not found."));
            return false;
        }

        if (!empty($request->description) && strlen($request->description) > 72) {
            $this->appendMessage(new Message("Description must be fewer than 72 characters."));
            return false;
        }

        $this->vendor_id = $vendor_id;
        $this->name = $request->name;
        $this->address1 = $request->address1;
        $this->address2 = empty($request->address2) ? "" : $request->address2;
        $this->city = $request -> city;
        $this->state_id = $request->state->id;
        $this->zip = $request->zip;
        $this->country_id = $request->country->id;
        $this->description = $request->description;
        $this->lat = $request->lat;
        $this->lng = $request->lng;

        return $this->save();
    }
}