Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column name must be either a string or an array yii

Tags:

php

yii

i got some stuck when accessing a yii's web application. I have configured as the same as the owner's setting, but while i tried to access, i got an error "Column must be either a string or an array". How could i solve it? Thanks in advance..

like image 682
syaloom Avatar asked Jan 11 '13 03:01

syaloom


4 Answers

When reporting error messages, it helps to have the precise error message. The actual error message is: "Column name must be either a string or an array". With an exact string you can search the framework files to find where it is mentioned.

Looks like some method somewhere is passing an invalid column name to createInCondition method of CDbCommandBuilder.

See line 722: https://github.com/yiisoft/yii/blob/1.1.13/framework/db/schema/CDbCommandBuilder.php

Looking at a couple instances where that method is called, I would guess that you have a database table without a primary key somewhere. That is one possible explanation for the problem. Other explanations will require a lot more details on your part.

Provide the stack trace that the error page provides you with when in debug mode along with your table schema.

like image 105
Willem Renzema Avatar answered Oct 22 '22 16:10

Willem Renzema


This happens when you don't have a primary key in your table and you try to do an update. I got this problem because I had a composite primary key in my table. I was being handled well on all operations until I wanted to update a model.

Just add an int primary key, call it 'id' to your table with auto increment. It should do the trick.

Be sure to disable schema caching (if you're using that) before you test this. The change wont take effect until your schema cache expires.

like image 23
Laith Avatar answered Oct 22 '22 16:10

Laith


Maybe you do not have primary key in your table. If you use the method $model->save() to save or use method $model->update() ($model is CActiveRecord instance), you will get this error.

Because the method update in CActiveRecord using Primary key to update (Read more here )

Source Code: framework/db/ar/CActiveRecord.php#1115

if($this->_pk===null)
        $this->_pk=$this->getPrimaryKey();
$this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes));
$this->_pk=$this->getPrimaryKey();

You can use method updateAll() instead of update() or updateByPk()

like image 35
dakiquang Avatar answered Oct 22 '22 15:10

dakiquang


Take a look this link

http://www.yiiframework.com/forum/index.php/topic/3887-cdbexception-column-name-must-be-either-a-string-or-an-array/

It seems your table doesn't have a primary key or the primary key doesn't well restored which usually caused by corrupt back up file.

like image 2
Wildan Muhlis Avatar answered Oct 22 '22 14:10

Wildan Muhlis