Yii Active Record Not Working Properly

[font="Arial,"]I created a a model using Gii code generator to ensure that I will not have any mistakes in creating my models. and this was the code that was generated.[/font]

[font="Arial,"]

[/font]

[font="Arial,"]




/**

 * This is the model class for table "siteconfig".

 *

 * The followings are the available columns in table 'siteconfig':

 * @property integer $config_id

 * @property string $key

 * @property string $value

 * @property string $update_time

 */

class SiteConfigurationRecord extends CActiveRecord

{

    /**

 	* Returns the static model of the specified AR class.

 	* @param string $className active record class name.

 	* @return SiteConfigurationRecord the static model class

 	*/

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

 	* @return string the associated database table name

 	*/

    public function tableName()

    {

        return 'siteconfig';

    }


    /**

 	* @return array validation rules for model attributes.

 	*/

    public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('key, value, update_time', 'required'),

            array('key', 'length', 'max'=>255),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('config_id, key, value, update_time', 'safe', 'on'=>'search'),

        );

    }


    /**

 	* @return array relational rules.

 	*/

    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

        );

    }


    /**

 	* @return array customized attribute labels (name=>label)

 	*/

    public function attributeLabels()

    {

        return array(

            'config_id' => 'Config',

            'key' => 'Key',

            'value' => 'Value',

            'update_time' => 'Update Time',

        );

    }


    /**

 	* Retrieves a list of models based on the current search/filter conditions.

 	* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

 	*/

    public function search()

    {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria=new CDbCriteria;


        $criteria->compare('config_id',$this->config_id);

        $criteria->compare('key',$this->key,true);

        $criteria->compare('value',$this->value,true);

        $criteria->compare('update_time',$this->update_time,true);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }

}

?>

[/font]

[font="Arial,"]

[/font]

[font="Arial,"]so i use this model in a controller in this way[/font]

[font="Arial,"]


$etona = new SiteConfigurationRecord();

            $crit = new CDbCriteria();

            $crit->select = "value";

            $crit->condition = "key=:key";

            $crit->params = array(":key"=>"sitename");

            $etona = $etona->find($crit);

[/font]

[font="Arial,"]

[/font]

[font="Arial,"]But a strange error occurs[/font]

[font="Arial,"]

[/font]

[font="Arial,"]


CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='sitename' LIMIT 1' at line 1. The SQL statement executed was: SELECT value FROM siteconfig t WHERE key=:key LIMIT 1

[/font]

[font="Arial,"]

[/font]

[font=Arial,]Where I went wrong? please help me[/font]

Hi mahan,

"key" is a reserved word. You have to escape it.




    $crit->condition = "`key`=:key";

    $crit->params = array(":key"=>"sitename");




Or, use compare() to let yii do it for you.




    $crit->compare('key', "sitename");