AR and multi-column primary key

Hello

I have 2 columns in the table defined as primary key (id, gallery_category_id) under one name of the key - PRIMARY.

For tables management I’m using CActiveRecord which as the primary key gets the second column - gallery_category_id. How can I force Yii to use id column instead? I tried setPrimaryKey with no success.

override the getPrimaryKey() of the model…

Thanks for advise, that helped. But another problem occurred.

After I override getPrimaryKey() with


    

public function getPrimaryKey() {

        $pk = parent::getPrimaryKey();

        return $pk['id'];

    }



while running $this->_model = Gallery::model()->findbyPk($_GET[‘id’]); I got below error. Any ideas how to fix it?

PHP Error

Opis

Invalid argument supplied for foreach()

Plik źródłowy

../yii/framework/db/schema/CDbCommandBuilder.php(669)

00657: $values[$i][$name]=$db->quoteValue($value);

00658: else

00659: $values[$i][$name]=$value;

00660: }

00661: else

00662: throw new CDbException(Yii::t(‘yii’,‘The value for the column “{column}” is not supplied when querying the table “{table}”.’,

00663: array(’{table}’=>$table->name,’{column}’=>$name)));

00664: }

00665: }

00666: if(count($values)===1)

00667: {

00668: $entries=array();

00669: foreach($values[0] as $name=>$value)

00670: $entries[]=$prefix.$table->columns[$name]->rawName.($value===null?’ IS NULL’:’=’.$value);

00671: return implode(’ AND ',$entries);

00672: }

00673:

00674: return $this->createCompositeInCondition($table,$values,$prefix);

00675: }

00676: else

00677: throw new CDbException(Yii::t(‘yii’,‘Column name must be either a string or an array.’));

00678: }

00679:

00680: /**

00681: * Generates the expression for selecting rows with specified composite key values.

Stack Trace

#0 /home/centrum/yii/framework/db/schema/CDbCommandBuilder.php(472): CDbCommandBuilder->createInCondition()

#1 /home/centrum/yii/framework/db/ar/CActiveRecord.php(1294): CDbCommandBuilder->createPkCriteria()

#2 /home/centrum/public_html/panel/protected/controllers/GaleriaController.php(219): Gallery->findByPk()

#3 /home/centrum/public_html/panel/protected/controllers/GaleriaController.php(92): GaleriaController->loadModelGallery()

#4 /home/centrum/yii/framework/web/actions/CInlineAction.php(32): GaleriaController->actionEdytuj_zdjecie()

#5 /home/centrum/yii/framework/web/CController.php(300): CInlineAction->run()

#6 /home/centrum/yii/framework/web/filters/CFilterChain.php(129): GaleriaController->runAction()

#7 /home/centrum/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()

#8 /home/centrum/yii/framework/web/CController.php(1049): CAccessControlFilter->filter()

#9 /home/centrum/yii/framework/web/filters/CInlineFilter.php(59): GaleriaController->filterAccessControl()

#10 /home/centrum/yii/framework/web/filters/CFilterChain.php(126): CInlineFilter->filter()

#11 /home/centrum/yii/framework/web/CController.php(283): CFilterChain->run()

#12 /home/centrum/yii/framework/web/CController.php(257): GaleriaController->runActionWithFilters()

#13 /home/centrum/yii/framework/web/CWebApplication.php(324): GaleriaController->run()

#14 /home/centrum/yii/framework/web/CWebApplication.php(121): CWebApplication->runController()

#15 /home/centrum/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()

#16 /home/centrum/public_html/panel/index.php(13): CWebApplication->run()

Sorry, I should think twice and check docs before I garbage forum with my question.

Of course findbyPk() method requires to specify values for every primary key in the table and I specified only one.