0
0

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 1 year has passed since last update.

【cakePHP】任意の順番でソートがしたい。

Posted at

DESC とか ASC でソートじゃ物足りない時。

テーブル名.フィールド名が[2]の時、4番目
テーブル名.フィールド名が[0]の時、2番目
テーブル名.フィールド名が[3]の時、1番目
その他の時、3番目

のような並び順にしたい。↓

$query->order(function ($exp, $q) {
    return $exp->addCase(
    [
        $q->newExpr()->eq('テーブル名.フィールド名', 2),
        $q->newExpr()->eq('テーブル名.フィールド名', 0),
        $q->newExpr()->eq('テーブル名.フィールド名', 3),
    ],
    [4, 2, 1, 3],
    ['integer', 'integer', 'integer', 'integer']
);

参考・参照
https://stackoverflow.com/questions/47032368/how-to-implement-case-when-under-order-by-using-cakephp-3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?