Yii Framework Forum: Model Search() Is Getting Variables That Aren't Sent - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Model Search() Is Getting Variables That Aren't Sent Rate Topic: -----

#1 User is offline   Kuodo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 05-January 13

Posted 07 January 2013 - 01:43 AM

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 :)
0

#2 User is offline   Kuodo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 05-January 13

Posted 07 January 2013 - 02:12 AM

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?
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,045
  • Joined: 16-February 11
  • Location:Japan

Posted 07 January 2013 - 02:59 AM

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.
0

#4 User is offline   Kuodo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 05-January 13

Posted 07 January 2013 - 04:43 AM

Oh, thanks :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users