Yii Framework Forum: AR and multi-column primary key - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

AR and multi-column primary key Rate Topic: -----

#1 User is offline   Zorba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-June 10

Posted 14 July 2010 - 04:33 AM

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.
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 14 July 2010 - 07:30 AM

override the getPrimaryKey() of the model...
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#3 User is offline   Zorba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-June 10

Posted 16 July 2010 - 09:14 AM

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()
0

#4 User is offline   Zorba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-June 10

Posted 16 July 2010 - 09:33 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users