回答編集履歴

1

newExprは省略できるのでより簡易な表現に変更

2019/10/21 04:41

投稿

nojimage
nojimage

スコア957

test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
 
30
30
 
31
- これをCakePHPのクエリで組み立てるときは、`newExpr()`メソッドを使うとよいでょう
31
+ これをCakePHPのクエリでselectに指定ます
32
32
 
33
33
 
34
34
 
@@ -44,15 +44,11 @@
44
44
 
45
45
  ->leftJoinWith('Comments')
46
46
 
47
- ->select(static function (Query $q) {
47
+ ->select([
48
48
 
49
- return [
49
+ 'avg_of_repeat_rate' => '(Ices.repeat_rate + SUM(Comments.repeat_rate)) / (1 + COUNT(Comments.repeat_rate))',
50
50
 
51
- 'avg_of_repeat_rate' => $q->newExpr('(Ices.repeat_rate + SUM(Comments.repeat_rate)) / (1 + COUNT(Comments.repeat_rate))'),
52
-
53
- ];
51
+ ])
54
-
55
- })
56
52
 
57
53
  // そのままだと、 avg_of_repeat_rate がstringで返ってくるのでfloatにするよう型変換を指定
58
54