2017-02-09 62 views
0

我想列出公開狀態的帖子,朋友的帖子如何聯合使用匹配

讓朋友張貼

$friendsPosts= $this->Posts->find('all') 
     ->contain(['Users', 'Languages', 'PostStates']) 
     ->matching('Users.Dusers', function ($q) { 
      return $q->where(['Dusers.id' => $this->Auth->user('id')]); 
     }); 

讓公衆交

$posts= $this->Posts->find('all') 
     ->where(['Posts.post_state_id' => 3]) 
     ->contain(['Users', 'Languages', 'PostStates']); 

$posts->union($friendsPosts); 
dd($posts->toArray()); 

錯誤消息時:將使用SELECT報表具有不同數量的列

回答

0

這裏是解決方案

$posts = $this->Posts->find() 
       ->where(['Posts.post_state_id' => 3]) 
       ->contain(['Users', 'PostStates', 'Languages', 'Tags', 'Translations' => ['Users', 'Languages', 'conditions' => ['post_state_id' => 3]]]); 

     $friendsPosts = $this->Posts->find('all') 
       ->where(['Posts.post_state_id' => 2]) 
       ->contain(['PostStates', 'Languages', 'Users', 'Tags', 'Translations' => ['Users', 'Languages', 'conditions' => ['post_state_id' => 3]]]) 
       ->innerJoinWith('Users.Dusers', function ($q) { 
      return $q->where(['Dusers.id' => $this->Auth->user('id'), 'UsersUsers.status' => 1])->select($this->Posts->Users); 
     }); 
$posts->union($friendsPosts);