查询在cakephp 3中获取数据

时间:2016-09-21 03:23:39

标签: php mysql cakephp

我遇到查询cakephp3.x的问题。我想返回数据有像project_id返回project_id中的列表名称, 但是project_id是唯一的.unduplicated project_id。 抱歉,我的英语。请帮助。

$result = $query->find('all', [
                        'fields' => [
                            'project_id' => 't_project_member.project_id',
                            'project_name' => 't_project.name',
                            'member_name' => 'concat(t_member.first_name," ",t_member.last_name)'
                        ],
                        'join' => [
                            ['table' => 't_project', 
                            'type' => 'LEFT',
                            'conditions' => 't_project.id = t_project_member.project_id'],

                            ['table' => 't_member', 
                            'type' => 'LEFT',
                            'conditions' => 't_member.id = t_project_member.member_id'],

                        ],
                        'conditions' => ['t_project.id' => $project_id]
                ]);

结果:

 {
 "result": [
    {
        "project_id": 4,
        "project_name": "Ueno Rebrand : Business cards #1",
        "member_name": "User_3 c"
    },
    {
        "project_id": 4,
        "project_name": "Ueno Rebrand : Business cards #1",
        "member_name": "User_4 d"
    },
    {
        "project_id": 4,
        "project_name": "Ueno Rebrand : Business cards #1",
        "member_name": "User_5 e"
    },
  ]
}

我想要返回如下数据。

{
 "result": [
    {
        "project_id": 4,
        "project_name": "Ueno Rebrand : Business cards #1",
        "member_name": {
                        "User_3 c",
                        "User_4 d",
                        "User_5 e
                       }
    },
  ]
}

非常感谢。

1 个答案:

答案 0 :(得分:0)

$result = $query->find('all', [
                        'fields' => [
                            'project_id' => 't_project_member.project_id',
                            'project_name' => 't_project.name',
                            'member_name' => 'concat(t_member.first_name," ",t_member.last_name)'
                        ],
                        'join' => [
                            ['table' => 't_project', 
                            'type' => 'LEFT',
                            'conditions' => 't_project.id = t_project_member.project_id'],

                            ['table' => 't_member', 
                            'type' => 'LEFT',
                            'conditions' => 't_member.id = t_project_member.member_id'],

                        ],
                        'group' => '`t_project_member`.`project_id`',
                        'conditions' => ['t_project.id' => $project_id]
                ]);