Cakephp 可控制的韩元';带HABTM&;有许多

Cakephp 可控制的韩元';带HABTM&;有许多,cakephp,filter,has-and-belongs-to-many,containable,Cakephp,Filter,Has And Belongs To Many,Containable,我所拥有的: "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(

我所拥有的:

"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, ... )
    )
) 
我想要什么:

"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,我可以大大减少查询,但仍然存在相关的模型问题:

// 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:我读过,从书中,以及

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; 

ContaineableBehavior将自动返回映射结果所需的字段

引用您已经阅读过的一篇文章:

$this->Post->find('all', array('contain' => 'Comment.author'));

... // data returned:

[Comment] => Array
    (
        [0] => Array
            (
                [author] => Daniel
                [post_id] => 1
            )
...
如您所见,注释数组 仅包含作者字段(加上 CakePHP需要的post_id 以映射结果)


在HABTM关系的情况下,返回带有相关外键的联接模型,因为Containable需要
a_id
c_id
字段。我的建议是忽略它,接受你需要的价值观。如果您愿意,您可以查看一下,因为Containable有时会多次查询数据库。但是,关联模型的数据不会像可包含的那样返回。

对于那些寻求相同内容的人,我最终用$A=$data['A']格式化数组$B=Set::classicExtract($data,'B.{n}.{field1 | field2 | field3 | field4}')$数据=数组('A'=>A$B'=>B$B);