我正在将CakePHP用于我的项目...... 为了加快我的代码,我只想获得一些与我的实际对象相关的模型类型:
$result = $this->Category->find(
'first',
array(
'conditions' => array(
'Category.folder' => $path[0]
),
'contain' => array('Rubric',
'conditions' => array('Rubric.category_id' => 'Category.id'),
'contain' => array('Subrubric',
'conditions' => array('Subrubric.rubric_id' => 'Rubric.id')
)
)
)
);
path [0]是来自网址的参数...
找到了类别和规则,但子规则没有。还有与我的对象相关的条目,但我希望它们在我的Rubric视图中而不是在类别视图中。
模特关系:
类别:
public $hasMany = array(
'Rubric' => array(
'className' => 'Rubric',
'foreignKey' => 'category_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => 'title',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
专栏:
public $hasMany = array(
'Entrieslocation' => array(
'className' => 'Entrieslocation',
'foreignKey' => 'rubric_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Subrubric' => array(
'className' => 'Subrubric',
'foreignKey' => 'rubric_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
这里我不想要Entrylocations ......
答案 0 :(得分:3)
请参阅:Containing deeper associations
你不应该在更深层的关系中使用节点'包含'。您的代码应如下所示。
$result = $this->Category->find(
'first',
array(
'conditions' => array(
'Category.folder' => $path[0]
),
'contain' => array('Rubric',
'conditions' => array('Rubric.category_id' => 'Category.id'),
'Subrubric' => array(
'conditions' => array('Subrubric.rubric_id' => 'Rubric.id')
)
)
)
);
答案 1 :(得分:0)
您还可以使用另一种选择。
$this->ModelName->unbindModel(array
(
'hasMany' => array
(
'OtherModel'
)
));
$this->ModelName->bindModel(array
(
'belongsTo' => array
(
'SomeOtherModel' => array
(
'foreignKey' => false,
'conditions' => array
(
'ModelName.id = SomeOtherModel.foreignKey'
)
)
)
));