2010-11-29 2 views
0

私が持っているもの:収容可能で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 ... */) 
     ) 
    ) 
) 

収容可能使用して、私は、クエリにかなり減らすことができ、それでもモデル嫌なものが関連しています:

// 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:私は本からthisthis、および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

を試してみたContainableBehaviorは結果を自動的にマッピングするために必要なフィールドを返します。すでに読んpageから引用

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

... // data returned: 

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

あなたが見ることができるように、コメントアレイは が唯一の著者フィールドを含む(プラスマッピングするためのCakePHP によって必要とされる post_idの結果)。 HABTM関係の場合

a_idc_idフィールドが収容可能で必要とされるので、関連する外部キーとの結合モデルが、返されます。私の提案は、それを無視して必要な値を取ることです。必要に応じて、おそらくjoinsを見ることができます。なぜなら、ContainableはDBに何度も何度も問い合わせを行うことがあるからです。ただし、関連モデルのデータはContainableとしてうまく返されません。

+0

同じことをお探しの方には、$ A = $ data ['A']で配列をフォーマットしてしまいました。 $ B =セット:: classicExtract($データ、 'B. {n}。{フィールド1 |フィールド2 |フィールド3 |フィールド4}'); $ data = array( 'A' => $ A、 'B' => $ B ); –

関連する問題