0
現在、ある特定のテーブルで2つの列を同時に並べ替えることに問題があります。この場合、特定の列(modified_timeline)をソートする必要がありますが、この列がNULLの場合は別の列のタイムスタンプを使用してソート(作成)します。私は次のクエリを持っています:Cakephp 3 - 2列の列がヌルの場合
return $this->find()
->contain([
'Comments' => function ($q){
return $q
->contain(['Users' => function ($q) {
return $q->select($this->select);
}])
->order(['Comments.created' => 'ASC']);
},
'Users' => function ($q) {
return $q->select($this->select)
->contain(['Pictures' => function ($q) {
return $q->where(['Pictures.profile_photo' => true]);
}]);
},
'Albums'
])
->matching('Albums.Users', function ($q) use ($userId) {
return $q->where(['Users.id' => $userId]);
})
// This particular line:
->order([
'Pictures.modified_timeline' => 'DESC',
'Pictures.created' => 'ASC'
]);
この作業は、modified_timelineがnullになることを考慮しない方法で部分的にしか動作しません。私はこの問題を解決するためにcase式を使うことを考えていましたが、case式の中でisNull()関数をどのように適用するかを理解していません。したがって、
ORDER BY
CASE
WHEN Pictures.modified_timeline is null
THEN Pictures.created
ELSE Pictures.modified_timeline
END
私の質問は、CakePHP 3クエリービルダーで上のSQLコードを作成する方法について説明します。次のSQLコードは、私が欲しいものを説明しますか?詳細については
->order(function ($exp, $q) {
return $exp->addCase(
[
$q->newExpr()->eq('modified_timeline', null),
//$q->newExpr()->gt('modified_timeline', 0), // or you may change condition here
],
['Pictures.created','Pictures.modified_timeline'], // values matching conditions
['string', 'string'] // type of each value (optional)
);
を::ヨーヨーは、前述したように
CASE条件を直接order()メソッドに入れることができます: 'order([" CASE WHEN .... "])' – arilia