Home > Backend Development > PHP Problem > Phalcon framework soft deletion and physical deletion solutions

Phalcon framework soft deletion and physical deletion solutions

桉梓
Release: 2020-10-13 16:39:12
Original
417 people have browsed it

In project development, different businesses have different operations on data in the database. For example, some data tables need to physically delete data records, while some records in the data tables need to be fake deleted (soft deleted). Most of the frameworks used now have implemented these two operations on data. So how to use it in phalcon?

Physical deletion in phalcon $model->delete();

Use $model->delete() in phalcon; the default execution is physical deletion, then the data records in the database are deleted.

Soft deletion is used in phalcon

phalcon provides many functions similar to hooks. I understand that hooks are similar to the functions of middleware. There is a method in phalcon to add behaviors:

$this->addBehavior()
Copy after login
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Users extends Model{    
    const DELETED = "D";    
    const NOT_DELETED = "N";   
    public function initialize()
    {
        $this->addBehavior(
        new SoftDelete(
        [
            "field" => "status", // 数据库中的字段
            "value" => Users::DELETED, //修改数据库中status的值为D
        ]
            )
        );
    }
}
Copy after login

So, when using When using the $model->delete() method, the corresponding field in the data table is modified to status = 'D' to implement the soft delete function, but what are the problems with this? Think...

This This piece of code is in each model, redundant, and subsequent optimization... Then implement it in the BaseModel in the parent class you write. All models inherit from BaseModel and share it, which is good...

The above two points must be Everyone knows the advantages of doing this. So is there no problem with this?

Use trails to separate code

According to different businesses, the processing of data in the database is also different, and some models require Soft deletion, and some models need to be physically deleted. Use the following method:

 namespace xxxx;
 use Phalcon\Mvc\Model\Behavior\SoftDelete;
 if (!trait_exists('ModelSoftDeletes')) {    
     trait ModelSoftDeletes{
         public function delete(){
             $this->addBehavior(
                 new SoftDelete([
                 "field" => "is_deleted", 
                 "value" => self::DELETED
                ])
            );
            return parent::delete();
        }
    }
}
Copy after login

Use in the model that needs to use soft deletion:

class Userodel extend Model {    
    use ModelSoftDeletes;
    ......
}
Copy after login

The delete method in ModelSoftDeletes will overwrite the Model The delete method adds a soft deletion implementation to the current model before calling $model->delete().

The above is the detailed content of Phalcon framework soft deletion and physical deletion solutions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template