代码之家  ›  专栏  ›  技术社区  ›  Myer

Containable不会使用HABTM和hasMany过滤二级模型

  •  0
  • Myer  · 技术社区  · 15 年前

    我所拥有的:

    "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:

    // 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公司 :我读过 this , this ,和 this 从书上,以及 this 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; 
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   RabidFire    15 年前

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

    引用自 page 你已经读过:

    $this->Post->find('all', array('contain' => 'Comment.author'));
    
    ... // data returned:
    
    [Comment] => Array
        (
            [0] => Array
                (
                    [author] => Daniel
                    [post_id] => 1
                )
    ...
    

    如您所见,注释数组 只包含author字段(加 CakePHP所需的post_id 以映射结果)。

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