Save Checkboxlist Values In Database

Hi All,

I want to save multiple check-box values in one column of the database. Here I attached the screen shot. I want save it values to database. Please Help ME… And also I want Display it values from database…

Thanks in Advance.

Raaz3266

Untitled.png

Please Help $ME$…

Idea is convert data to JSON before validate, and convert data to array after find

In Model, save data to ‘colors’ column in db

in form

$fom->checkBoxList($model, ‘colors’, array(‘maroon’=>‘MAROON’,‘beige’=>‘BEIGE’));//TODO: correct data dropdown

protected function beforeValidate() {

if (!is_array($this->colors)) $this->colors = array();


$this->colors = CJSON::encode($this->colors);





return parent::beforeValidate();

}

protected function afterFind() {

parent::afterFind();


$this->colors = CJSON::decode($this->colors);


if (!is_array($this->colors)) $this->colors = array();

}

By coincidence needed exactly this. nice use of the hooks. I also liked the idea of json-encoding (i was going to csv)

Got curious, can you explain to me the position choice of parent calls and use/non-use of method return?




   protected function beforeValidate()

    {

		...

        return parent::beforeValidate();

    }


    protected function afterFind()

    {

        parent::afterFind();

		...

    }



Hi Thank you for your reply, But this code will save only "[]" in the database.

Do not know exactly what your problem is, as I do not know your current code.

But I implemented it myself yesterday and really works smooth. I will give you an example for use with active(!)ListBox where we have to introduce a virtual attribute $colors_selected to supply already selected colors to list box:

your model:




    public $colors_selected;


	...


    protected function afterFind()

    {

        parent::afterFind();


        $this->colors = CJSON::decode($this->colors);

        if (!is_array($this->colors)) {

            $this->colors = array();

        }

        $this->colors_selected=array();

        if(sizeof($this->colors)) {

            foreach($this->colors as $color) {

                $this->colors_selected[$color]=array( 'selected' => 'selected' );

            }

        }

    }


    protected function beforeValidate()

    {

        if (!is_array($this->colors)) {

            $this->colors = array();

        }

        $this->colors = CJSON::encode($this->colors);


        return parent::beforeValidate();

    }



your view:




echo $form->listBox($model,'colors',Colors::getColorOptions(), array('multiple'=>true, 'options' => $model->colors_selected ) );



This should work (not tested as I quickly adapted example for your "color" example)

Still curious about position of parent calls and use/non-use of method return. Anybody?

Thank you for your reply. But I want to view icons from another table, according the colors fields in the products table.

Also check this

http://www.yiiframework.com/wiki/681/update-related-models-of-a-model-using-listbox-and-checkboxlist-in-the-form/