LoginSignup
6
3

More than 5 years have passed since last update.

CakePHP3 SoftDeleteTrait 論理削除を取得

Last updated at Posted at 2017-05-30

概要

アソシエーション先のテーブルに SoftDelete プラグインが設定されている場合、
連結条件には ~ JOIN Articles ON Articles.deleted IS NULL が付加される。

下記のように記述すると論理削除済みの Articles も含めて取得できる。


Model

class ArticlesTable extends Table
{
    /* 論理削除設定 */
    use SoftDeleteTrait;
    protected $softDeleteField = 'deleted';
}

Controller

class RecipesController extends AppController
{

    /**
     * {@inheritDoc}
     */
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        // モデルのロード
        $this->loadModel('Articles');
    }


    public function index()
    {
        // 論理削除を含めて取得
        $this->Articles->find('all', ['withDeleted']);
    }

    public function index2()
    {
        // アソシエーション先の論理削除を含める
        $this->Recipes->find()
            ->where(['column' => 'condition'])
            ->contain(['Articles' => function ($q) {
                return $q->find('all', ['withDeleted']);
            }]);
    }
}
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3