سؤال

ماذا املك:

"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 ... */)
        )
    )
)

باستخدام احتواء ، يمكنني تقليل الاستعلام قليلاً ، ولكن لا يزال هناك طراز مرتبط بـ 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: أنا قد قرأت هذه, هذه, ، و هذه من الكتاب ، وكذلك هذه و هذه.

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; 
هل كانت مفيدة؟

المحلول

سيعيد ContableBehavior تلقائيًا الحقول المطلوبة لتعيين النتائج.

نقلاً عن أ صفحة التي قرأتها بالفعل:

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

... // data returned:

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

كما ترون ، تحتوي صفائف التعليق فقط على حقل المؤلف (بالإضافة إلى post_id الذي يحتاجه CakePhP لرسم خريطة النتائج).

في حالة علاقات HABTM ، يتم إرجاع نموذج الانضمام إلى المفاتيح الأجنبية المرتبطة به ، لأن a_id و c_id الحقول مطلوبة عن طريق احتواء. اقتراحي هو مجرد تجاهله واتخاذ القيم التي تحتاجها. إذا كنت تريد ، فربما يمكنك إلقاء نظرة على ينضم لأن احتواء في بعض الأحيان يستفسر ديسيبل عدة مرات. ومع ذلك ، لن يتم إرجاع بيانات النماذج المرتبطة بها بشكل جيد.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top