If:

Survey hasMany SurveyUser
User hasMany SurveyUser

SurveyUser belongsTo Survey
SurveyUser belongsTo User

with ContainableBehavior (see: http://book.cakephp.org/view/1323/Containable),
you can try something like the following in your controller:

  // Get list of all surveys, and any responses the user has made to
the surveys
  $surveys = $this->Survey->find('all', array(
    'contain' => array(
      'SurveyUser' => array(
        'conditions' => array(
          'SurveyUser.user_id' => $user_id
        )
      )
    )
  ));

The resulting array will be all the surveys, with a sub-array (index
name is 'SurveyUser'). If the user has not answered the survey, the
sub-array will be empty, but the surveys the user has answered will
also have a one-element sub-array containing the corresponding
SurveyUser row for that user (and you can use that to "grey-out" the
survey record on your view).

If you prefer to completely exclude the surveys that the user has
answered, you can always do something like:

  // Get list of surveys user has answered
  $answered = $this->Survey->SurveyUser->find('all', array(
    'conditions' => array(
      'SurveyUser.user_id' => $user_id
    )
  );
  // Get list of all surveys, excluding the ones answered by the user
  $surveys = $this->Survey->find('all', array(
    'conditions' => array(
      'Survey.id NOT IN (' . join(',', Set::extract('/SurveyUser/id',
$answered)) . ')'
    )
  );

Set::extract (see: http://book.cakephp.org/view/1501/extract) is used
to convert $answered to a simple array of just the ids of the
SurveyUser records.

Or you can combine both using method outlined in
http://book.cakephp.org/view/1030/Complex-Find-Conditions, under the
section "Sub-queries".

For more information on the model relationship types, see
http://book.cakephp.org/view/1039/Associations-Linking-Models-Together

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to