Wednesday, March 28, 2012

Re: multi-level Model associations behavior

On Tue, Mar 27, 2012 at 20:40, Augey Mikus <augeymikus@gmail.com> wrote:
> I am building an eLearning site in CakePHP that has models:  Course,
> Test, Question, Answer, and others.
>
> Here are my associations:
>
> Course hasMany Test
> Test belongsTo Course
> Question belongsTo Test
> Question hasMany Answer
> Answer belongsTo Question
>
> As you can see, the $test array in my view stops at Question, and
> doesn't pull the associated Answer(s) for the Question(s).
>

You could try to use the Containable Behaviour
(http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html)

$this->Test->find(
'all',
array(
'contain' => array(
'Course',
'Question' => array(
'Answer'
)
)
),
'conditions' => array(
'Test.id' => $id
)
);

That should bring back the proper array with the answers corresponding
to each question.

Mike.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: