在Cakephp4中加入查询仅返回一个表数据

问题描述

我正在尝试使用CakePHP4的联接查询来获取评论,但是我得到的结果是简单的Reviews表数据。我如何也可以附加用户表。

例如

public function getReviews()
{
    $this->request->allowMethod(['get']);
    
    //$authenticator = new JWTController();
    //$data        = $authenticator->requestAuthorization();

    $offset = 1;//$data['offset']; 
    
    $reviews = $this->getTableLocator()->get('Reviews');
    $result = $reviews
        ->find('all')
        ->join([
            'Users'=>  [
                'table'      => 'users','type'       => 'INNER','conditions' => 'Users.id = Reviews.userId',]
        ])
        ->order(['Reviews.created' => 'DESC'])
        ->limit(25)
        ->page($offset)->toArray();
            
    $this->jsonOutput(array(
        'error' => false,'limit' => 25,'data'  => $result
    ));
}
{
    "id": 1,"userId": 1,"rating": 4,"review": "How are you","created": "2020-10-11T23:14:44+00:00"
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)