我有一个HABTM关系效果很好,当我在find()
上Foo
时,我会得到这样的结果:
{
"Foo": {
"ID": "32",
...
},
"Bar": [
{
"ID": "3",
...
},
{
"ID": "4",
...
}
]
}
...但在某些情况下,我只希望Foo
没有相关的Bars
(甚至不查询Bars
)。我怎么能这样做?
我试过了:
$this->{$this->modelClass}->find('all', array('fields' => array($this->modelClass.'.*')));
......但它没有帮助。
如果重要,我的模型是:
class Foo extends AppModel {
public $primaryKey = "ID";
public $hasAndBelongsToMany = array(
'Bar' =>
array(
'className' => 'Bar',
'joinTable' => 'bars_foos',
'foreignKey' => 'foo_ID',
'associationForeignKey' => 'bar_ID'
)
);
}
class Bar extends AppModel {
public $primaryKey = "ID";
public $hasAndBelongsToMany = array(
'Foo' =>
array(
'className' => 'Foo',
'joinTable' => 'bars_foos',
'foreignKey' => 'bar_ID',
'associationForeignKey' => 'foo_ID'
)
);
}
答案 0 :(得分:0)
经过一番挖掘后,我在Model Attributes文档中找到了这个: http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive
就我而言,Model->find('all', array('recursive' => -1))
正在做我想做的事。