我正在尝试在分类列表中查找用户的杂货。关联是 Category hasMany Item hasMany User through Grocery。我正在使用 Containable Behavior,它没有过滤掉所有其他 Grocery。它基本上返回每个项目。
我的控制器功能:
function showlist() {
$categories = $this->Category->find('all', array(
'contain' => array(
'Item' => array(
'Grocery' => array(
'conditions' => array(
'Grocery.user_id =' => $this->Auth->user('id')
)
)
)
)
));
返回的数组:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 10
[parent_id] =>
[name] => Dairy
[lft] => 1
[rght] => 6
)
[Item] => Array
(
)
)
[1] => Array
(
[Category] => Array
(
[id] => 11
[parent_id] => 10
[name] => Milk
[lft] => 2
[rght] => 3
)
**[Item] => Array
(
)**
)
[2] => Array
(
[Category] => Array
(
[id] => 12
[parent_id] => 10
[name] => Cheese
[lft] => 4
[rght] => 5
)
[Item] => Array
(
)
)
[3] => Array
我不想返回下面没有 Grocery(ItemUser) 的任何 Item 数组。
这有效,但现在它只显示平坦。我需要它来显示该嵌套数组中某一类别的所有项目。
它显示:
Array
(
[0] => Array
(
[Category] => Array **<--This could be the same category as**
(...)
[Item] => Array
(...)
[Grocery] => Array
(...)
)
[1] => Array
(
[Category] => Array **<--This**
(...)
[Item] => Array
(...)
[Grocery] => Array
(...)
)
[2]...
我需要它是:
Array
(
[0] => Array
(
[Category] => Array
(...
[Item] => Array
(...)
[Item] => Array
(...)
)
)
[1] => Array
(
[Category] => Array
(...)
[Item] => Array
(...)
)
[2]...
甚至不需要 Grocery 数组,只需通过它们搜索条件即可。