Trouble updating a CActiveRecord instance

Hello,

I've been having a bit of trouble updating a specific model type in my application. All our other CActiveRecord inherited classes work fine, but one specific one throws an exception whenever I try to update, save, refresh, etc.

The CDbException's description is "Column name must be either a string or an array." and appears to be thrown at CDbCommandBuilder->createInCondition.

I really can't see anything that makes this one table any different from the rest, but if more details are needed I can provide them. I am hoping that someone at least knows the general reason for that specific exception so I can work with that.

Thanks!

As a temporary solution, I have found I can update the instance by calling updateAll with the condition containing the primary keys of the instance. This doesn't seem like the best way to do this, so help would still be appreciated!

What is the complete error call stack?

#0 C:\wamp\www\yiiapp\framework\db\schema\CDbCommandBuilder.php(414): CDbCommandBuilder->createInCondition(Object(CMysqlTableSchema), NULL, Array)

#1 C:\wamp\www\yiiapp\framework\db\ar\CActiveRecord.php(1483): CDbCommandBuilder->createPkCriteria(Object(CMysqlTableSchema), NULL, '', Array)

#2 C:\wamp\www\yiiapp\framework\db\ar\CActiveRecord.php(1126): CActiveRecord->updateByPk(NULL, Array)

#3 C:\wamp\www\yiiapp\frying_pan\protected\modules\TestModule\controllers\LU_gamesController.php(253): CActiveRecord->update()

#4 C:\wamp\www\yiiapp\frying_pan\protected\modules\TestModule\controllers\LU_gamesController.php(228): LU_gamesController->calculateScore('27')

#5 C:\wamp\www\yiiapp\framework\web\actions\CInlineAction.php(32): LU_gamesController->actionTestScore()

#6 C:\wamp\www\yiiapp\framework\web\CController.php(279): CInlineAction->run()

#7 C:\wamp\www\yiiapp\framework\web\filters\CFilterChain.php(129): CController->runAction(Object(CInlineAction))

#8 C:\wamp\www\yiiapp\framework\web\filters\CFilter.php(41): CFilterChain->run()

#9 C:\wamp\www\yiiapp\framework\web\CController.php(917): CFilter->filter(Object(CFilterChain))

#10 C:\wamp\www\yiiapp\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#11 C:\wamp\www\yiiapp\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))

#12 C:\wamp\www\yiiapp\framework\web\CController.php(262): CFilterChain->run()

#13 C:\wamp\www\yiiapp\framework\web\CController.php(236): CController->runActionWithFilters(Object(CInlineAction), Array)

#14 C:\wamp\www\yiiapp\framework\web\CWebApplication.php(332): CController->run('testScore')

#15 C:\wamp\www\yiiapp\framework\web\CWebApplication.php(120): CWebApplication->runController('TestModule/l…')

#16 C:\wamp\www\yiiapp\framework\base\CApplication.php(133): CWebApplication->processRequest()

#17 C:\wamp\www\yiiapp\frying_pan\index.php(11): CApplication->run()

#18 {main}

The error says that you are trying to save a record whose PK is NULL.

Hmm, strange. In the database the pk fields are set to not null, and both of them are filled out when I print_r the instance. Anything I am missing?

Maybe you can show the code you use to update the record?

[tt] $completedQuestions = LU_games_questions::model()->findAll(

		array(


			'condition'=>'game_id=:game_id '


			.'AND host_answer IS NOT NULL '


			.'AND host_guess IS NOT NULL '


			.'AND player2_answer IS NOT NULL '


			.'AND player2_guess IS NOT NULL',


			'params'=>array(


				':game_id'=>$d['game']->game_id


			)


		)


	);


	


	$q = $completedQuestions[0]; //if I run print_r($q); here, everything looks fine


	$q->refresh(); //could also be $q->update(), $q->save(), etc[/tt]

In the code above you select it but not update it.

yeah, I was just trying to show with the comment that if I do refresh, update, save, etc, it always throws the same error. if I was to add

[tt]$q->host_guess = 'test';

$q->update(array('host_guess'));[/tt]

the same error would be thrown

Does your table have a primary key defined?

It appears it doesn't!  This is probably the problem, haha. The way the table is working now there is no field we could use as a pk, but there are 2 fields that have indexes. It is possible to update based on a composite key?

Yes, composite PK is supported. If you don't have PK defined in your table, you must declare it in your model's primayKey() method.

okay, thanks!

I have this now:

[tt]public function getPrimaryKey()

{


	return array('game_id'=>$this->game_id, 'question_id'=>$this->question_id);


}[/tt]

and I can print_r the getPrimaryKey() function and it looks fine, but still throws the same error when I try updating. Am I missing something?

It's primaryKey(), not getPrimaryKey().

oops, sorry!

now the problem is

Property "LU_games_questions.game_id" is not defined.

Neither $this->game_id or $this->getAttribute('game_id') seem to work, what is the reason behind this? Sorry about all these problems coming up!

primaryKey() should return array('game_id', 'question_id'). This is for declaration purpose. You don't have access to $this->game_id yet.

oh okay sorry, I guess I just misinterpreted the class documentation on that function. It seems to work now, thanks alot!