PHPackages                             funayaki/cakephp3-soft-delete - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. funayaki/cakephp3-soft-delete

ActiveCakephp-plugin[Database &amp; ORM](/categories/database)

funayaki/cakephp3-soft-delete
=============================

SoftDelete plugin for CakePHP

2.0.0(7y ago)13.1k1[1 issues](https://github.com/funayaki/cakephp3-soft-delete/issues)MITPHPPHP &gt;=5.4

Since Feb 1Pushed 7y ago1 watchersCompare

[ Source](https://github.com/funayaki/cakephp3-soft-delete)[ Packagist](https://packagist.org/packages/funayaki/cakephp3-soft-delete)[ Docs](https://github.com/pgbi/cakephp3-soft-delete)[ RSS](/packages/funayaki-cakephp3-soft-delete/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (13)Used By (0)

CakeSoftDelete plugin for CakePHP
=================================

[](#cakesoftdelete-plugin-for-cakephp)

[![Build Status](https://camo.githubusercontent.com/0d3522c5e2a38834111d8ab244a866dc27db78925688550a70de7196fa961d69/68747470733a2f2f7472617669732d63692e6f72672f66756e6179616b692f63616b65706870332d736f66742d64656c6574652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/funayaki/cakephp3-soft-delete)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1449c625b01bb4d70de05bf0285cb230c7cff269eb6dfa667dc52509301e2efb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f66756e6179616b692f63616b65706870332d736f66742d64656c6574652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/funayaki/cakephp3-soft-delete/?branch=master)[![codecov](https://camo.githubusercontent.com/b856c31c00ce4926ae8dce119ff73eb4099da997f64bc31b6b1453eec2541555/68747470733a2f2f636f6465636f762e696f2f67682f66756e6179616b692f63616b65706870332d736f66742d64656c6574652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/funayaki/cakephp3-soft-delete)

Purpose
-------

[](#purpose)

This CakePHP plugin enables you to make your models soft deletable. When soft deleting an entity, it is not actually removed from your database. Instead, a `deleted` timestamp is set on the record.

Requirements
------------

[](#requirements)

- CakePHP 3.5.0 or later

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).

Update your composer file to include this plugin:

```
composer require funayaki/cakephp3-soft-delete
```

Configuration
-------------

[](#configuration)

### Load the plugin:

[](#load-the-plugin)

```
// In /config/bootstrap.php
Plugin::load('SoftDelete');
```

### Make a model soft deleteable:

[](#make-a-model-soft-deleteable)

Use the `SoftDeleteTrait` and implement the `SoftDeleteAwareInterface` on your model Table class:

```
// in src/Model/Table/UsersTable.php
...
use SoftDelete\Model\Table\Entity\SoftDeleteAwareInterface;
use SoftDelete\Model\Table\SoftDeleteTrait;

class UsersTable extends Table implements SoftDeleteAwareInterface
{
    use SoftDeleteTrait;

    public function getSoftDeleteField()
    {
        return 'deleted';
    }

    public function getSoftDeleteValue()
    {
        return date('Y-m-d H:i:s');
    }

    public function getRestoreValue()
    {
        return null;
    }
}
```

Use
---

[](#use)

### Soft deleting records

[](#soft-deleting-records)

`delete` and `deleteAll` functions will now soft delete records by populating `deleted` field with the date of the deletion.

```
// in src/Model/Table/UsersTable.php
$this->delete($user); // $user entity is now soft deleted if UsersTable uses SoftDeleteTrait.
```

### Restoring Soft deleted records

[](#restoring-soft-deleted-records)

To restore a soft deleted entity into an active state, use the `restore` method:

```
// in src/Model/Table/UsersTable.php
// Let's suppose $user #1 is soft deleted.
$user = $this->Users->find('all', ['withDeleted'])->where('id', 1)->first();
$this->restore($user); // $user #1 is now restored.
```

### Finding records

[](#finding-records)

`find`, `get` or dynamic finders (such as `findById`) will only return non soft deleted records. To also return soft deleted records, `$options` must contain `'withDeleted'`. Example:

```
// in src/Model/Table/UsersTable.php
$nonSoftDeletedRecords = $this->find('all');
$allRecords            = $this->find('all', ['withDeleted']);
```

### Hard deleting records

[](#hard-deleting-records)

To hard delete a single entity:

```
// in src/Model/Table/UsersTable.php
$user = $this->get($userId);
$success = $this->hardDelete($user);
```

To mass hard delete records that were soft deleted before a given date, you can use hardDeleteAll($date):

```
// in src/Model/Table/UsersTable.php
$date = new \DateTime('some date');
$affectedRowsCount = $this->hardDeleteAll([
    $this->getSoftDeleteField() . '
