可容纳将不会过滤二级模型与HABTM&有很多 [英] Containable won't filter 2nd level model with HABTM & hasMany
问题描述
我有什么:
"A" HABTM "C" HABTM "A" through join table "B"
"A" hasMany "B" belongsTo "A"
"C" is ordered by a "B" field
我想要的:
// result:
[0] => array(
A => array( /* single model's fields I still need*/ ),
C => array(
[0] => array( C.field1, C.field2, ... /* Model C fields*/ ),
[1] => array( C.field1, C.field2, ... )
)
)
我尝试过的:
// this gives me data I don't need:
A->find('all', array( 'conditions' => array( 'id' => $id ) ) )
// result:
[0] => array(
A => array( /* single model's fields I need*/ ),
B => array( /* I DON'T NEED */
[0] => array( ... )
[1] => array( /* ... etc, tons records I don't need */ )
),
C => array(
[0] => array( C.field1, C.field2, ... /* I need these fields*/
[B] => array( /* I DON'T NEED */ )
),
[1] => array( C.field1, C.field2, ... )
[B] => array( /* ... etc, each has a model B I don't need ... */)
)
)
)
使用Containable,我可以减少查询,但仍然有关联的模型cruft:
Using Containable, I can cut down the query quite a bit, but there's still associated model cruft:
// this is a little better
A->find('all', array(
'conditions' => array( 'id' => $id ),
'contain' => array( 'C' )
))
// result:
[0] => array(
A => array( /* single model's fields I still need*/ ),
C => array(
[0] => array( C.field1, C.field2, ... /* I still need Model C fields*/
[B] => array( /* I still DON'T need this Model's fields */ )
),
[1] => array( C.field1, C.field2, ...
[B] => array( /* ... still has unneeded model B */)
)
)
)
NB1: I've read this, this, and this from the book, as well as this and this.
NB2 :我也尝试过
C->recursive = -1 // no effect
C->unbindModel(array('hasAndBelongsToMany'=>A)) // no effect
A->find('all', array( // not what I want, but it's still
'conditions' => array('id' => $id), // odd that this doesn't filter C's
'contain' => array('A.B'))); // fields out. same as second result
A->find('all', array( // also not what I want, but it's
'conditions' => array('id' => $id), // weird that this doesn't filter B's
'contain' => array('A.B.field'))); // fields at all;
推荐答案
ContainableBehavior将自动返回映射所需的字段
The ContainableBehavior will automatically return fields that are required to map results.
引用 page :
$this->Post->find('all', array('contain' => 'Comment.author'));
... // data returned:
[Comment] => Array
(
[0] => Array
(
[author] => Daniel
[post_id] => 1
)
...
只包含作者字段(加上
,它是CakePHP
映射结果所需的post_id)。
As you can see, the Comment arrays only contain the author field (plus the post_id which is needed by CakePHP to map the results).
在HABTM关系的情况下,返回具有关联外键的连接模型,因为 a_id
和 c_id
字段是Containable所必需的,我的建议是忽略它,并采取你需要的值如果你想,你可以看看 join ,因为Containable有时会查询DB多次,但是,关联模型的数据不会作为Containable很好地返回。
In the case of HABTM relationships, the join model with the associated foreign keys is returned, since the a_id
and c_id
fields are required by Containable. My suggestion is to just ignore it and take the values you need. If you want, you could probably take a look at joins because Containable sometimes queries the DB many, many times. However, the data for associated models won't be returned as nicely as Containable.
这篇关于可容纳将不会过滤二级模型与HABTM&有很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!