cakephp3にSoftDeleteを実装する

1、アプリケーションのディレクトリにsoftdeleteのプライグインをインストールする

composer require pgbi/cakephp3-soft-delete "~1.0"

2、bootstrap.phpにsoftdeleteプラグインの読み込みを記述する

※CakePHP 3.6 以降の記述方法
\App\Application::addPlugin('SoftDelete');
※CakePHP 3.5 以前の記述方法
Plugin::load('SoftDelete');

3、modelに以下の2文を記述すればok

<?php
namespace App\Model\Table\Baked;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use App\Model\Table\AppTable;
use SoftDelete\Model\Table\SoftDeleteTrait;  -- 追記


class UsersTable extends AppTable
{
   use SoftDeleteTrait;  -- 追記

   public function initialize(array $config)
   {
       parent::initialize($config);
       $this->setTable('users');
       $this->setDisplayField('name');
       $this->setPrimaryKey('id');
       $this->belongsTo('Rooms', [
           'foreignKey' => 'room_id',
       ]);
   }

この記事が気に入ったらサポートをしてみませんか?