How do you save a multiselect in a Yii model?

Practice




how do I save a multiselect select in a Yii model?

in the model, add
public function afterFind()
{

if ($this->data_multiselect=='') $this->data_multiselect=array();
else $this->data_multiselect= unserialize($this->data_multiselect);
return parent::afterFind();
}


public function beforeSave()
{
$this->data_multiselect_sub= serialize($this->data_multiselect);
return parent::beforeSave();
}





in the controller

if (isset($_POST['LUser']['data_multiselect']))
$model_user->data_multiselect= $_POST['LUser']['data_multiselect'];
else ($model_user->data_multiselect = array());




in the view
$types[id]=array(1=>'first', 2=>'second', 3=>'third');

echo $form->listBox($model, 'data_multiselec', $types, array('multiple' => 'multiple' ));



and of course define the field in the database as a string
in the model's rules()
public function rules() {
return array(
array('field names, field names,data_multiselec', 'safe', 'on' => 'search', ),
array('field names, data_multiselec', 'default', 'value' => 0),
array('avatar', 'file', 'allowEmpty'=>true, 'types'=>'jpg, gif, png'),
);
}


in the model's public function search()


...
$criteria->compare('data_multiselec', $this->data_multiselec);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));



and of course, the property in the model

public $data_multiselec;

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Famworks"

Terms: Famworks