Error With Cgridview

Hey all,

I have a cgridview which populates values from a model called messagesnew but when i try the search option on attributes read, to, from , i get a error as attached

The relations of my tables are:-

My messagesnew relation:




CREATE TABLE `messagesnew` (

 `messageId` int(11) NOT NULL AUTO_INCREMENT,

 `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

 `read` tinyint(1) NOT NULL,

 `to` varchar(45) DEFAULT NULL,

 `from` varchar(45) DEFAULT NULL,

 `title` varchar(45) DEFAULT NULL,

 `content` varchar(45) DEFAULT NULL,

 PRIMARY KEY (`messageId`),

 KEY `fk_messagesnew_logindetailskasun_idx` (`to`),

 CONSTRAINT `fk_messagesnew_logindetailskasun` FOREIGN KEY (`to`) REFERENCES `logindetailskasun` (`Username`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1



My logindetailskasun relation




CREATE TABLE `logindetailskasun` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `Password` varchar(45) DEFAULT NULL,

 `Username` varchar(45) DEFAULT NULL,

 `level` varchar(45) DEFAULT NULL,

 PRIMARY KEY (`id`),

 UNIQUE KEY `Username` (`Username`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1



I have created the models for both my relations

relations for each table:-




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(

			'to0' => array(self::BELONGS_TO, 'Logindetailskasun', 'to'),

		);

	}






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(

			'messagesnews' => array(self::HAS_MANY, 'Messagesnew', 'to'),

		);

	}



action admin of my Messagesnew:-




public function actionAdmin()

	{

		$model=new Messagesnew('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Messagesnew']))

			$model->attributes=$_GET['Messagesnew'];


		$this->render('admin',array(

			'model'=>$model,

		));

	}



The search for messagesnew is as follows :-




public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;


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

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

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

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

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

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

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



use


$criteria->compare('t.messageId',$this->messageId);



Note the "t" before messageId

Hi

u can change the serch function


public function search()

        {

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

                // should not be searched.


                $criteria=new CDbCriteria;


                //$criteria->compare('messageId',$this->messageId);

                if(isset($_POST['messagesnews'])){

                        //here u can write the custom query

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

                }

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

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

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

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

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

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


                return new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                ));

        }

‘to’ is a reserved word in MySQL, and is not quoted in the resulting query (hence the error [font=“Courier New”]WHERE to LIKE ‘%STF01%’[/font])

Having searching the doc quickly, I also believe CDbCriteria doesn’t provide an option to quote field names.

So you may want to rename that field, or use the addCondition() method instead of the compare() one

I mean, instead of:


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

use this


$criteria->addCondition('`to` LIKE :to');

$criteria->params = array(

    ':to' => "%{$this->to}%",

);



More explanation: http://www.yiiframework.com/wiki/199/creating-a-parameterized-like-query/