从cakephp中的两层父模型中选择字段


select fields from two level parent model in cakephp

假设我有三个模型命名为Content, Category, ZoneContentCategory模型的子模型,CategoryZone模型的子模型。层次结构如下图

Zone
---Category
------Content

现在我想从包含父模型的一些字段的Content表中获取数据。我可以从直接父模型中获取数据但不能从父模型中选择父模型

我试了什么?
这是我尝试的查询

$result = $this->Content->find('all', array('recursive' => 2, 'contain' => array('Category.Zone'), 'fields' => 'Content.name, Category.name, Category.Zone.name')); //problem is Category.Zone.name otherwise all is ok

这个查询可以从Content和它的父模型Category中获取数据,但是不能从Category的父模型Zone中获取数据。

注意:我可以通过连接来实现。但是我想通过关联来实现

注意:一旦使用Containable Behavior,递归将被忽略。参见Docs

$result = $this->Content->find('all', array(
    'contain' => array(
        'Category' => array('Zone')
    ), 
    'fields' => 'Content.name, Category.name, Zone.name'
));

您已经通过了'contain' => array('Category.Zone'), Model应该有它们的on键,并且您可以选择设置关联来代替值。如:-

'contain' => array(
    'Category' => array(/*conditions*/),
    'Zone' => array(/*conditions*/)
), 

我假设你已经在模型中做了关联,所以没有必要在这里传递