Yii Error: Foreach($This->_Pkalias As $Name=>$Alias), Cactivr Record


I am currently trying to use findAll() method in the CActiveRecord. Unfortunately, Yii is giving me the following error;

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

772     {

773         // determine the primary key value

774         if(is_string($this->_pkAlias))  // single key

775         {

776             if(isset($row[$this->_pkAlias]))

777                 $pk=$row[$this->_pkAlias];

778             else    // no matching related objects

779                 return null;

780         }

781         else // is_array, composite key

782         {

783             $pk=array();

784             foreach($this->_pkAlias as $name=>$alias)

785             {

786                 if(isset($row[$alias]))

787                     $pk[$name]=$row[$alias];

788                 else    // no matching related objects

789                     return null;

790             }

791             $pk=serialize($pk);

792         }


794         // retrieve or populate the record according to the primary key value

795         if(isset($this->records[$pk]))

796             $record=$this->records[$pk];

Looking this up, it is due to the fact that my table (an intermediate table linking two other tables), does not have a primary key set. When I try to set one in the DB (using InnoDB tables), the database won’t allow me to do so because of duplicate entries. I currently have 2 columns in the table, set as indexes.

I would like to know how to set these primary keys using Yii (and where (which file), as I am new to Yii). I also saw people mentioning on other help forums about altering the CActiveRecord to not require a primary key, but as I am new to Yii, I am not sure I would want to attempt this.

Thank you in advance for your help!!!


You could define a composite primary key for the intermediate table:

CREATE TABLE `post_category` (

  `post_id` int(11) NOT NULL,

  `category_id` int(11) NOT NULL,

  PRIMARY KEY (`post_id`,`category_id`)


Thank you!