1

I am new to yii framework.

            <div class="col-md-5">
          <input id="ytAdd_Employee_varHobbies" type="hidden" value="" name="Add_Employee[varHobbies]">
          <span id="Add_Employee_varHobbies">
          <input id="Add_Employee_varHobbies_0" value="5" type="checkbox" name="Add_Employee[varHobbies][]">
          <label for="Add_Employee_varHobbies_0">BaseBall</label>
          <br>
          <input id="Add_Employee_varHobbies_1" value="2" type="checkbox" name="Add_Employee[varHobbies][]">
          <label for="Add_Employee_varHobbies_1">Football</label>
          <br>
          <input id="Add_Employee_varHobbies_2" value="4" type="checkbox" name="Add_Employee[varHobbies][]">
          <label for="Add_Employee_varHobbies_2">Hockey</label>
          <br></div>

Here i have multiple checboxes as array in the name of Add_Employee[varHobbies][]

In the controller i saved the form as

 $model->attributes=$_POST['Add_Employee'];
         //print_r($model);exit;
         $image_upload=$model->Image = CUploadedFile::getInstance($model,'Image'); 

         if( $model->validate() && $model->save())
         {  }

By using $model->save() all my fields are saved properly but this check box saved as array.

In my model i set the rules as

array('varHobbies', 'type', 'type' => 'array', 'allowEmpty' => false),

I want to save the hobbies as 1,2,3 and so on....

But it is saved as 'array'.

How to fix this?

Please help me

1 Answer 1

1

Add this to your model to save hobbies as a string:

protected function beforeSave() {
    parent::beforeSave();
    $this->varHobbies = implode(',', $this->varHobbies);
    return $this;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.