0

I've had some problems joining two tables with CActiveDataProvider

The tables are: question(id,description) , questionInFeedback(feedbackId,questionId)

and the relations :

feedback=>

'questionInFeedbacks' => array(self::HAS_MANY,'QuestionInFeedback','feedbackId'),

question=>

'questionInFeedbacks' => array(self::HAS_MANY,'QuestionInFeedback','questionId'),

questionInFeedback=>

'feedback' => array(self::BELONGS_TO, 'Feedback', 'feedbackId'),
'question' => array(self::BELONGS_TO, 'Question', 'questionId'),

if I could use SQL then i'd use

SELECT q.id
FROM questionInFeedback AS qf,Question AS q
WHERE qf.question_id=q.id

I need these Questions ID to display in feedback view the question which are related to the feedback.

protected/controllers/feedbackController:

$issueDataProvider=new CActiveDataProvider('Question', array(
            'criteria'=>array(
                'condition'=>'',
                'params'=>array(':questionId'=>$this->loadModel($id)->id),

Thanks for the help :)

EDIT : Yii is giving me the following error;

"Invalid argument supplied for foreach()", with line 826 being highlighted.

{
815         // determine the primary key value
816         if(is_string($this->_pkAlias))  // single key
817         {
818             if(isset($row[$this->_pkAlias]))
819                 $pk=$row[$this->_pkAlias];
820             else    // no matching related objects
821                 return null;
822         }
823         else // is_array, composite key
824         {
825             $pk=array();
826             foreach($this->_pkAlias as $name=>$alias)
827             {
828                 if(isset($row[$alias]))
829                     $pk[$name]=$row[$alias];
830                 else    // no matching related objects
831                     return null;
832             }
833             $pk=serialize($pk);
834         }
835 
836         // retrieve or populate the record according to the primary key value
837         if(isset($this->records[$pk]))
838             $record=$this->records[$pk];
Arik
  • 55
  • 7

1 Answers1

0

You can use the with attribute of your criteria to specify the relation:

$issueDataProvider=new CActiveDataProvider('Question', array(
    'criteria'=>array(
        'condition'=>'t.id = :questionId',
        'params'=>array(':questionId'=>$this->loadModel($id)->id),
         'with' => array('questionInFeedbacks'),
    )
);

By the way not sure about your loadModel method, but chances are that $this->loadModel($id)->id will return the same value as $id

So 'params'=>array(':questionId'=>$this->loadModel($id)->id), could be changed to 'params'=>array(':questionId'=>$id),

darkheir
  • 8,844
  • 6
  • 45
  • 66
  • Hey , thanks for the quick answer. the problem is now something about "alias" - not sure what that means. the error : Invalid argument supplied for foreach() – Arik Jul 28 '14 at 10:28
  • Could you edit you question with the error? (with the stack trace) – darkheir Jul 28 '14 at 11:33
  • And what SQL request yii has executed? – darkheir Jul 28 '14 at 13:35
  • Hey, thanks for the help. the SQL executed is : SELECT COUNT(DISTINCT `t`.`id`) FROM `question` `t` LEFT OUTER JOIN `question_in_feedback` `questionInFeedbacks` ON (`questionInFeedbacks`.`questionId`=`t`.`id`) WHERE (t.id = :questionId) – Arik Jul 29 '14 at 06:59
  • Maybe the problem is that one of my tables has a composite key and not a PK?(there is no PK in questionInFeedback) – Arik Jul 29 '14 at 07:02