使用匹配时如何结合

时间:2017-02-09 08:37:35

标签: union cakephp-3.0 matching

我正在尝试列出公开状态帖子和朋友帖子

收到朋友发帖

$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语句具有不同的列数

1 个答案:

答案 0 :(得分: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);