On Tue, Aug 10, 2010 at 8:28 PM, Olwen Williams
<[email protected]> wrote:
> I'm working on a survey app:
> Models Survey, Question, ResponseOption, Response, QuestionResponse
> One side is that Survey has several Questions, and Question may have several
> ResponseOptions (these are multichoice or a number of check boxes)
> These other side is that a Survey has many Responses, and a Response has
> many QuestionResponses (which are for a question, and optionally for a
> ResponseOption).
> In my Survey Controller I'm doing the following to only read the Survey
> Side:
> $this->Survey->recursive = 2;
> $this->Survey->unbindModel(array('hasMany' => array('Response')));
> $this->Survey->Question->unbindModel(array('belongsTo' => array('Survey')));
> $this->Survey->Question->unbindModel(array('hasMany' =>
> array('QuestionResponse')));
> $this->Survey->Question->ResponseOption->unbindModel(array('hasMany' =>
> array('QuestionResponse')));
> $survey = $this->Survey->read(null, $id);
> Is there a better way to do this?
> If I want to also (in the same method) read the one response and it's
> associated QuestionResponses should I just unbind the associations from
>  $this->Survey->Response?
>

Use ContainableBehavior, which will take care of unbinding your
models, as well as the recursive value.

class Survey extends AppModel
{
        public $actsAs = array('Containable');
        
        ...
        
        
controller:

$data = $this->Survey->find(
        'first',
        array(
                'conditions' => array(...),
                'contain' => array(
                        'Question' => array(
                                'fields' => array(...),
                                'ResponseOption' => array(
                                        'fields' => array(...)
                                )
                        )
                )
        )
);

Notice that ResponseOption is inside the array for Question, as it's
associated with that model, not Survey. Use debug($data) to see how
the array is returned.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to