duanbodai5166 2017-02-08 03:31
浏览 12
已采纳

如何正确执行CakePHP 3中的LEFT连接?

I have been attempting to use CakePHP 3's query builder format in order to learn how to better use it. However, at the moment I cannot seem to get a left join working.

There is information available in both tables that I would like to collect in the result:

    $query = $this->find()->where(['userID' => $UID])->hydrate(false)->join([
        'table' => 'authgroups',
        'alias' => 'a',
        'type' => 'LEFT',
        'conditions' => 'a.id = userGroup'
    ]);

    return $query->toArray();

If I'm reading the CookBook correctly, this should work - however, it is being used in a model which differs from the example, and the result only returns from that table, seemingly ignoring the join. Do I need to execute this in the controller instead?

  • 写回答

1条回答 默认 最新

  • doumu1212 2017-02-08 04:12
    关注

    This works in model:

    What I guess is your join query might working well.The important thing in cakephp join is that:

    If you are joining table 'A' with table 'B' from table 'A' ,
    By default it will select all fields from table A and no fields from table B.
    

    You need to select some fields from another table as well:

    $query = $this->find()->where(['userID' => $UID])->hydrate(false)->join([
        'table' => 'authgroups',
        'alias' => 'a',
        'type' => 'LEFT',
        'conditions' => 'a.id = userGroup'
     ])->autoFields(true) // selecting all fields from current table
       ->select(["a.field1,a.field2"]); // selecting some fields from table authgroups
    
    return $query->toArray();
    }
    

    And I don't know if these things are provided well in cakephp documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题