获取没有(HABTM)关联数据的数据
Get data without (HABTM) associated data
我有一个运行良好的 HABTM 关系,当我在 Foo
上执行 find()
时,我得到这样的结果:
{
"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'
)
);
}
经过一番挖掘,我在模型属性文档中找到了这个:
http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive
就我而言,Model->find('all', array('recursive' => -1))
正在做我想做的事。
我有一个运行良好的 HABTM 关系,当我在 Foo
上执行 find()
时,我得到这样的结果:
{
"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'
)
);
}
经过一番挖掘,我在模型属性文档中找到了这个: http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive
就我而言,Model->find('all', array('recursive' => -1))
正在做我想做的事。