GSTAR
(Omzy83)
December 23, 2009, 12:49pm
1
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?
mbi
(mbi)
December 23, 2009, 12:57pm
2
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)
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?
I can tell you not to do
not separate fields, because add o remove one is difficult to manage
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]
not separate fields, because add o remove one is difficult to manage
GSTAR
(Omzy83)
December 23, 2009, 1:37pm
4
I can tell you not to do
not separate fields, because add o remove one is
array in database? how?
I don’t understand?
By array I meant comma separated values, eg: [value1, value2, value3]
Then use php explode()/implode() function to extract the values
GSTAR
(Omzy83)
January 18, 2010, 9:38am
6
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()); ?>
Spyros
(Spyros)
January 18, 2010, 9:55am
7
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
GSTAR
(Omzy83)
January 18, 2010, 10:18am
8
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?
Spyros
(Spyros)
January 18, 2010, 11:11am
9
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