0

私が持っているもの:

"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; 
4

1 に答える 1

1

ContainableBehaviorは、結果をマップするために必要なフィールドを自動的に返します。

すでに読んだページからの引用:

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

... // data returned:

[Comment] => Array
    (
        [0] => Array
            (
                [author] => Daniel
                [post_id] => 1
            )
...

ご覧のとおり、Comment配列には、作成者フィールド(およびCakePHPが結果をマップするために必要なpost_id)のみが含まれています。

a_idHABTM関係の場合、Containableではフィールドとc_idフィールドが必須であるため、関連する外部キーを持つ結合モデルが返されます。私の提案は、それを無視して、必要な値を取ることです。ContainableはDBに何度もクエリを実行することがあるため、必要に応じて、結合を確認することもできます。ただし、関連するモデルのデータは、Containableほど適切に返されません。

于 2010-11-29T03:55:29.867 に答える