2016-09-21 3 views
0

У меня проблема с запросом cakephp3.x. Я хочу, чтобы возвращаемые данные имели вид, как project_id, возвращают имя списка внутри project_id, Но project_id уникален .unduplicated project_id. извините мой английский.пожалуйста, помогите.Запрос получить данные в cakephp 3

$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 
         } 
    }, 
    ] 
} 

спасибо.

+0

Group By 'project_id', может оказаться, что помощь – Karan

+0

Возможная Дубликат [CakePHP и GROUP BY] (http://stackoverflow.com/questions/2929666/cakephp-and-group-by) –

ответ

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] 
       ]);