Model Search() Is Getting Variables That Aren't Sent

Hello.

I’m learning Yii for a couple of days. And everything was good until I got this problem, that google doesn’t know anything about.

I have a model name MUser:




class MUser extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return User 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 'user';

	}


	/**

	 * @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('email, password, name, roles', 'required'),

			array('password', 'length', 'max'=>32),

			array('email, name', 'length', 'max'=>64),

			array('roles', 'length', 'max'=>13),

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

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

			array('email, name, roles', '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(

			'id' => 'ID',

		);

	}


	/**

	 * 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('id',$this->id);

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

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

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


		echo $this->roles;


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



And I have a controller that has actionIndex:




public function actionIndex()

	{

		$model = new MUser();

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

	}



And a view file "list":




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'user-grid',

	'dataProvider'=>$model->search(),

	'filter' => $model,

	'itemsCssClass' => 'table table-bordered table-minified-input items',

	'columns'=>array(

		array(

			'id' => 'name', 'name' => 'name', 'filter' => false, 'value' => $model->name

		),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



And everytime I refresh the page, $model->roles gets set to ‘user’ so search() starts searching users with ‘user’ roles’

Help needed here. Thanks :)

Found out that it is setting $model->roles to the default row value from table.

But why is it so and how to get rid of it?




public function actionIndex()

	{

		$model = new MUser();

		$model->unsetAttributes();

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

	}



new MUser() will return a MUser model with default values. You have to clear them by usetAttributes() when you use the model as a search parameter holder. You will see the same line in the actionAdmin controller that has been created by Gii.

Oh, thanks :)