[CDbException] Column name must be either a string or an array.

Dear all,

I use the following setup to try out Yii Framework:

Windows XP Pro

Apache 2.2.9

PHP 5.3.0

Yii 1.0.8

MySQL 5.0.67

I follow the example in "The Definitive Guide to Yii 1.0" to create the "testdrive" application. I was able to login and view the list of users in the database.

However, when I tried to update an entry with the following URL, I get the exception shown at the end of my question below:

http://127.0.0.1/yii/test/index.php?r=user/show&id=1

I found a thread in this forum with the similar exception, which is solved by creating primary key. In my case, the primary key has already been created.

What have I missed out?

Thanks in advance.

CDbException

Description

Column name must be either a string or an array.

Source File

C:\DATA\workspace\yii\framework\db\schema\CDbCommandBuilder.php(628)

00616: }

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

00618: {

00619: $entries=array();

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

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

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

00623: }

00624:

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

00626: }

00627: else

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

00629: }

00630:

00631: /**

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

00633: * @param CDbTableSchema the table schema

00634: * @param array list of primary key values to be selected within

00635: * @param string column prefix (ended with dot)

00636: * @return string the expression for selection

00637: * @since 1.0.4

00638: */

00639: protected function createCompositeInCondition($table,$values,$prefix)

00640: {

Stack Trace

#0 C:\DATA\workspace\yii\framework\db\schema\CDbCommandBuilder.php(429): CDbCommandBuilder->createInCondition(Object(CMysqlTableSchema), NULL, Array)

#1 C:\DATA\workspace\yii\framework\db\ar\CActiveRecord.php(1464): CDbCommandBuilder->createPkCriteria(Object(CMysqlTableSchema), ‘1’, ‘’, Array)

#2 C:\DATA\workspace\yii\test\protected\controllers\UserController.php(163): CActiveRecord->findByPk(‘1’)

#3 C:\DATA\workspace\yii\test\protected\controllers\UserController.php(58): UserController->loadUser()

#4 C:\DATA\workspace\yii\framework\web\actions\CInlineAction.php(32): UserController->actionShow()

#5 C:\DATA\workspace\yii\framework\web\CController.php(300): CInlineAction->run()

#6 C:\DATA\workspace\yii\framework\web\filters\CFilterChain.php(129): CController->runAction(Object(CInlineAction))

#7 C:\DATA\workspace\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#8 C:\DATA\workspace\yii\framework\web\CController.php(952): CFilter->filter(Object(CFilterChain))

#9 C:\DATA\workspace\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#10 C:\DATA\workspace\yii\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))

#11 C:\DATA\workspace\yii\framework\web\CController.php(283): CFilterChain->run()

#12 C:\DATA\workspace\yii\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#13 C:\DATA\workspace\yii\framework\web\CWebApplication.php(332): CController->run(‘show’)

#14 C:\DATA\workspace\yii\framework\web\CWebApplication.php(120): CWebApplication->runController(‘user/show’)

#15 C:\DATA\workspace\yii\framework\base\CApplication.php(134): CWebApplication->processRequest()

#16 C:\DATA\workspace\yii\test\index.php(11): CApplication->run()

show please code from user/show

Hi,

Please see below. This is generated by yiic.

/**


 * Shows a particular model.


 */


public function actionShow()


{


	$this->render('show',array('model'=>$this->loadUser()));


}

BTW, when I var_dump($this->getTableSchema()), the output shows that there is no primary key even though there is primary defined on the column "id". Please see the table dump below.

Thanks.

object(CMysqlTableSchema)#25 (9) { [“schemaName”]=> NULL [“name”]=> string(4) “User” [“rawName”]=> string(6) “User” [“primaryKey”]=> NULL [“sequenceName”]=> NULL [“foreignKeys”]=> array(0) { } [“columns”]=> array(4) { [“id”]=> object(CMysqlColumnSchema)#26 (13) { [“name”]=> string(2) “id” [“rawName”]=> string(4) “id” [“allowNull”]=> bool(false) [“dbType”]=> NULL [“type”]=> string(6) “string” [“defaultValue”]=> NULL [“size”]=> NULL [“precision”]=> NULL [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“username”]=> object(CMysqlColumnSchema)#27 (13) { [“name”]=> string(8) “username” [“rawName”]=> string(10) “username” [“allowNull”]=> bool(false) [“dbType”]=> NULL [“type”]=> string(6) “string” [“defaultValue”]=> NULL [“size”]=> NULL [“precision”]=> NULL [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“password”]=> object(CMysqlColumnSchema)#28 (13) { [“name”]=> string(8) “password” [“rawName”]=> string(10) “password” [“allowNull”]=> bool(false) [“dbType”]=> NULL [“type”]=> string(6) “string” [“defaultValue”]=> NULL [“size”]=> NULL [“precision”]=> NULL [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } [“email”]=> object(CMysqlColumnSchema)#29 (13) { [“name”]=> string(5) “email” [“rawName”]=> string(7) “email” [“allowNull”]=> bool(false) [“dbType”]=> NULL [“type”]=> string(6) “string” [“defaultValue”]=> NULL [“size”]=> NULL [“precision”]=> NULL [“scale”]=> NULL [“isPrimaryKey”]=> bool(false) [“isForeignKey”]=> bool(false) ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL } } ["_e":“CComponent”:private]=> NULL ["_m":“CComponent”:private]=> NULL }

Table structure:

================

Table structure for table ‘user’

CREATE TABLE /!32312 IF NOT EXISTS/ "user" (

"id" int(11) NOT NULL auto_increment,

"username" varchar(128) NOT NULL,

"password" varchar(128) NOT NULL,

"email" varchar(128) NOT NULL,

PRIMARY KEY ("id")

) AUTO_INCREMENT=3 COMMENT=‘InnoDB free: 9216 kB; InnoDB free: 9216 kB’;

can you show code from show view?

may be you trying to get some relation with other table?

Hi,

There is only 1 table.

Below is the code for show:

<h2>View User <?php echo $model->id; ?></h2>

<div class="actionBar">

[<?php echo CHtml::link(‘User List’,array(‘list’)); ?>]

[<?php echo CHtml::link(‘New User’,array(‘create’)); ?>]

[<?php echo CHtml::link(‘Update User’,array(‘update’,‘id’=>$model->id)); ?>]

[<?php echo CHtml::linkButton(‘Delete User’,array(‘submit’=>array(‘delete’,‘id’=>$model->id),‘confirm’=>‘Are you sure?’)); ?>

]

[<?php echo CHtml::link(‘Manage User’,array(‘admin’)); ?>]

</div>

<table class="dataGrid">

<tr>

&lt;th class=&quot;label&quot;&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;getAttributeLabel('username')); ?&gt;

</th>

&lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;username); ?&gt;

</td>

</tr>

<tr>

&lt;th class=&quot;label&quot;&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;getAttributeLabel('password')); ?&gt;

</th>

&lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;password); ?&gt;

</td>

</tr>

<tr>

&lt;th class=&quot;label&quot;&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;getAttributeLabel('email')); ?&gt;

</th>

&lt;td&gt;&lt;?php echo CHtml::encode(&#036;model-&gt;email); ?&gt;

</td>

</tr>

</table>

And loadUser() function as well, please (should be ib the controller).

The following is generated by yiic.

/**


 * Returns the data model based on the primary key given in the GET variable.


 * If the data model is not found, an HTTP exception will be raised.


 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable


 */


public function loadUser(&#036;id=null)


{


	if(&#036;this-&gt;_model===null)


	{


		if(&#036;id&#33;==null || isset(&#036;_GET['id']))


			&#036;this-&gt;_model=User::model()-&gt;findbyPk(&#036;id&#33;==null ? &#036;id : &#036;_GET['id']);


		if(&#036;this-&gt;_model===null)


			throw new CHttpException(404,'The requested page does not exist.');


	}


	return &#036;this-&gt;_model;


}

can you get come info from user, for example:


$m = User::model()->findbyPk(1);

echo $m->username;



?

Hi,

I solved the problem, sorta.

I need to change the following function in User model to return the primary key column name. It’s strange that MySQL does not return this primary key automatically.

public function primaryKey() {


	return array('id');


}

it’s strange, because Yii make


SHOW COLUMNS FROM `User`

to get PK

look at the db log and try to run this command directly in MySQL