我在cakephp 3中使用选项分组。 我正在使用这个发现:
$seasons = $this->Episodes->Seasons
->find('all')
->combine('id','season','show_id')
->toArray();
结果几乎我需要的东西:
[
(int) 269 => [
(int) 784 => (int) 1
],
(int) 270 => [
(int) 785 => (int) 1,
(int) 786 => (int) 2,
(int) 787 => (int) 3
]
]
但是不是按show_id进行分组,而是按照节目的名称进行分组,即访问show_id字段关联的表并从那里获取名称,从而产生
[
'some show' => [
(int) 784 => (int) 1
],
'another show' => [
(int) 785 => (int) 1,
(int) 786 => (int) 2,
(int) 787 => (int) 3
]
]
我试过了:
$seasons = $this->Episodes->Seasons
->find('all')
->contain('Shows')
->combine('id','season','Shows.name')
->toArray();
但这不起作用。 (Shows.name似乎为空,导致根本没有分组。)如何实现所需的结果?
答案 0 :(得分:1)
回答我自己的问题:可以通过闭包来完成。
$seasons = $this->Episodes->Seasons
->find('all')
->contain('Shows')
->combine('id','season',function($season) {return $season->show->name;})
->toArray();
给出了期望的结果。