Checkboxlist Gives Me A "array To String" Error

I’m somewhat new to PHP and Yii so bear with me!

I have a multi part form, all textFields except this bit




#...

	<div class="row">

	    <?php echo $form->labelEx($model,'boatclass'); ?>

            <?php echo $form->checkBoxList($model,'boatclass', $model->boatClassList()); ?>

            <?php echo $form->error($model,'boatclass'); ?>

	</div>

#...



The checkBoxList values are populated from a function in my model,


public function boatClassList()

    {

        return array(

            'c1'=>'C1 Racing',

            'c1stock'=>'C1 Stock',

            'c2am'=>'C2 Proboat',

            'c2stock'=>'C2 Stock',

            'k1'=>'K1',

            'k2'=>'K2',

            'c4'=>'C4'

        );

    }

The data from this checkBoxList is supposed to be saved to a single field in a mysql database. Now, my own understanding of the problem is that I need to somehow implode the options the user selected in the checkBoxList into a string before it’s saved into the database. I have not been able to successfully implement this though. A single checkBox works fine, it’s just the checkBoxList that’s tripping me up.

My current “fix” for it is a hacky bit of code where I use a bunch of separate single checkBox fields and combine the data from those into a string before saving it into the DB, but I’d like to find out if there’s as better/more correct way.

Thanks for your help.

Common way of saving checkboxes is using many-to-many with pivot table.

Anyway, you can use beforeSave() handler or setter to implode your values to string.

I suppose restructuring the database could be an option. Might as well do it right. More reading!!

In case anyone was wondering. I decided to go with overriding beforeSave() to implode the values in the checkBoxList array element in $_POST.


protected function beforeSave()

    {

        $this->boatclass=implode(', ', $this->boatclass);

        return parent::beforeSave();

    }