###前提・実現したいこと
cakephp3でtwitterぽいものを作成しています。followテーブルでid,user_id,follow_idの3つをフィールドに持っています。自分がフォローしているユーザーのツイートを取得したいので、以下のようにコードを作成しました。
PostsController.php
1public function index(){ 2 $this->set('user',$this->Auth->user()); 3 $this->set('entity',$this->Posts->newEntity()); 4 5 // 自分のフォローしている人たちのidを取得 6 $follow = $this->Follows->find('all') 7 ->where(['user_id'=>$this->Auth->user('id')]) 8 ->contain(['Users']); 9 10 // 自分のidを取得 11 $id = [$this->Auth->user('id')]; 12 13 $follow = $follow->toArray(); 14 15 for ($i=0; $i < count($follow); $i++) { 16 $num = $follow[$i]['follow_id']; 17 // 自分のidとフォローしている人のidを配列にまとめる 18 array_push($id,$num); 19 20 } 21 22 for ($i=0; $i < count($id); $i++) { 23 $conditions = $this->Posts->find() 24 ->where(['user_id'=>$id[$i]]) 25 ->contain(['Users']); 26 } 27 28 $data = $this->paginate($conditions); 29 $this->set('data',$data); 30 }
上記のコードで、$idに、自分とフォローしているユーザーのidを配列で入れて、それらをSQLbuilderで指定して、自分とフォローしているユーザーのツイートを取得しようかと考えていました。
しかし、$idが配列だとSQLbuilderで使えないので、上記のコードが上手くいきません。
どうしたらいいでしょうか。
アドバイスよろしくお願いいたします。
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/10 06:48