Capturing multiple selections

This isn’t strictly Yii related - more to do with object orientated design / database principles.

Basically I’m creating a form that has a set of checkboxes (about 75 in total), the user can have multiple selections here. How should the database be designed to store multiple selections? Do I have one field that stores the selections in an array, or do I create separate fields in the database for each checkbox?

What is the recommended way of doing this in Yii?

this is an m:n - scenario

a Post can have multiple tags, a tag belongs to multiple posts

post(id, title, text)

tag(id, name)

post_tag(postId, tagId)

I can tell you not to do

  1. not separate fields, because add o remove one is difficult to manage

  2. array in database? how?

maybe

table checkboxes

                           id


                           chk_name


                           chk_label

table user_setting

                            id


                            checkboxes_id (relation)


                            user_id (relation)

and the view is render with foreach checkboxes and set checked if exists in user_setting

maybe…

[edit]

  1. not separate fields, because add o remove one is difficult to manage
  1. I don’t understand?

  2. By array I meant comma separated values, eg: [value1, value2, value3]

Then use php explode()/implode() function to extract the values

ok, sorry

  1. difficult to manage

  2. ok, work too . relationship is not required

                        but is a poor design
    
    
                        the array could contain values that do not exist in checkboxes

Let’s suppose I have table for checkboxes.

How can I easily retrieve the values and re-populate model form :

Controller:


public function actionUpdate()

{

	$model=$this->loadApplication();		

	

	// processing code


	$this->render('update', array('model'=>$model));

}


public function loadApplication($id=null)

{

	if($this->_model===null)

	{

		if(isset($_GET['id'])) $id=$_GET['id'];

		if(isset($_POST['Application']['app_id'])) $id=$_POST['Application']['app_id'];

			

		if(isset($id))

		{

			$this->_model=Application::model()->findByPk($id);

		}


	return $this->_model;

}

View:


<?php echo CHtml::activeCheckBoxList($model, 'extra_information', Application::model()->getExtraInformationOptions()); ?>

For simple cases I use this approach

I have a database field name eg fields where I save a serialized array .

Then in the form I use




<?php echo CHtml::checkBoxList("fields", unserialize($model->fields),

        Job::$FIELDS,

        array("separator"=>"","checkAll"=>"Όλα","template"=>"<div style='float:left'>{label}{input}</div>")); ?>



where Job::$FIELDS is the array that holds all the checkboxes values like this




<?php 

 static $FIELDS = array("surname"=>"surname","name"=>"name","email"=>"email",

    "gender"=>"gender","address"=>"address","tk"=>"tk",

    "city"=>"city","area"=>"area","telephones"=>"telephones",

    "birthday"=>"birthday","nameday"=>"nameday","family"=>"family",

    "children"=>"children","occupation"=>"occupation","coupon"=>"coupon");

?>



Then in the controller you can

$model->fields = serialize($_POST["fields"]);

and then save the model

But if you use serialize then it makes it more difficult to do queries on the field. Also isn’t it easier to just use implode() to save the data and then explode() to output it?

That’s why I said on simple cases. I don’t need to query the fields. I just want to display them.

implode / explode won’t work if you choose a delimiter that may be in a field’s data