質問編集履歴

2

変更

2018/02/18 14:21

投稿

nanamin
nanamin

スコア27

test CHANGED
@@ -1 +1 @@
1
- cakephp3で他テーブルと結合したり、条件を設定したりする方法
1
+ cakephp3で結合後、他テーブルのカラムの条件で取得した
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 他テーブルと結合して、取得条件を設定したい。
3
+ 他テーブルと結合して、他テーブルのカラムで取得条件を設定したい。
4
4
 
5
5
  公式ドキュメントを見ているのですが、containとinnserjoinの使い方がいまいちよくわかっていません。
6
6
 
@@ -46,30 +46,18 @@
46
46
 
47
47
  //ArticlesController.php
48
48
 
49
- //一覧取得
49
+ //userのロール条件で取得
50
50
 
51
-
52
-
53
- $articles = $this->Articles->find('all',contain =>['users','colors']);
51
+ $articles = $this->Articles->find('all')->contain(['users','colors'])->where(['role' => 1]);
54
52
 
55
53
  $this->set('articles', $articles);
56
54
 
57
- debug($articles);
58
55
 
59
-
60
-
61
- //userのロール条件で取得
62
-
63
- $articles = $this->Articles->find('all',contain =>['users','colors'])->where(['role' => 1]);
64
-
65
- $this->set('articles', $articles);
66
-
67
- debug($articles);
68
56
 
69
57
 
70
58
 
71
59
  ```
72
60
 
61
+ これをすると、Error: Call to undefined method App\Controller\ArticlesController::where() と出ます。
73
62
 
74
-
75
- これではだめのでしょうか。例えば他テーブルに同一のカラム名(ここではusers.nameとcolors.nameある)の場合、どうオブジェクトを呼び出してしていったらよいのでしょうか。
63
+ ぜwhereが使えなくなってしったのでしょうか。

1

追記

2018/02/18 14:21

投稿

nanamin
nanamin

スコア27

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  //userのロール条件で取得
62
62
 
63
- $this->Articles->find('all',contain =>['users','colors'])->where(['role' => 1]);
63
+ $articles = $this->Articles->find('all',contain =>['users','colors'])->where(['role' => 1]);
64
64
 
65
65
  $this->set('articles', $articles);
66
66