1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP Sequence plugin でレコードの順番を管理

Posted at

公式

概要

レコードの順番を管理するシンプルなプラグインです。

インストール

composerでインストール後、config/bootstrap.phpに 追加。

composer require admad/cakephp-sequence
config/bootstrap.php
Plugin::load('ADmad/Sequence');

使い方

以下をBehaviorの設定に追加。

Table.php
$this->addBehavior('ADmad/Sequence.Sequence', [
    'order' => 'position',   // 順番を保存するカラム名。デフォルト: "position".
    'scope' => ['group_id'], // グルーピングに使うカラムの配列。 デフォルト [].
    'start' => 1,            // 順番の初期値。デフォルト 1.
]);

新しいレコードを保存する度に、'position' カラムに 1->2->3 と自動で順番が保存されます。

また、クエリのorder()が未指定の場合は、自動でorder('position asc')が追加されます。

保存、更新、削除時の動作

新規追加だけでなく、挿入や削除にも対応しています。
挿入や更新の場合は、positionに挿入、更新したい位置をセットしてsave()すれば、他のレコードのpositionも更新されます。
このため、大量のレコードがあるものには向かないのではないかと思われます。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?