CakePHP plugin for soft deleting
, (*1)
This Delete plugin enable soft deletable.
Entities aren't removed from your database. Instead, a deleted timestamp is set on the record., (*2)
Requirements
It is developed for CakePHP 4.x., (*3)
Installation
You can install plugin into your CakePHP application using composer., (*4)
The recommended way to install composer packages is:, (*5)
composer require slicesofcake/delete
Load the Behavior
Load the Behavior in your src/Model/Table/YourTable.php (or if you have AppTable.php). The default field named in database table should be deleted
(like created
or modified
) and compatible type with Time::now()
(eg. DATE
or DATETIME
)., (*6)
public function initialize(array $config)
{
parent::initialize($config);
$this->addBehavior('SlicesCake/Delete.Deleted');
}
You can configuration to customize the Delete plugin:, (*7)
$this->addBehavior('SlicesCake/Delete.Deleted', [
'field' => 'deleted_at', // Change column field name
]);
Add column by Migrations plugin
- Load Migrations plugin (https://github.com/cakephp/migrations).
- Copy file from /vendor/slicesofcake/delete/config/20200101122906_AddDeletedToProducts.example.php to your app main config directory.
- Rename to 20200101122906_AddDeletedTo[YourTable].php.
- Edit file and rename class with changes above.
- Update
change
method to set table name.
- Run migrations by command:
cake migrations migrate