Checkboxlist to Save Multiple Records

Hello,

I am newly use YII. I want to use checkboxlist to save multiple records. For example there is column/field named "order_number" and "service". One "order_number" can have many "services".

So, this is a form example:

Order_number: …

Service:

a. …

b. …

c. …

d. …

When I choose order_number (1) and service (a,b,c) click submit/create, I want to save in database like below:

id order_number service

1 1 a

2 1 b

3 1 c

Is there any solution to solve my problem?

I’m no expert in yii, but after googling this over half an hour, I ended up doing it manually.

Basically it would depend on what are you planning to do with the data. It’s either that you combine the whole array into the same variable using implode




$model = new Post();


if(isset($_POST['Post']))

   $model->attributes = $_POST['Post'];


   $valid = $model->validate();    //<-- I'm just guessing this is how you validate >.<


   if($valid)

       $model->yourCheckBoxItems = implode(",",$model->yourCheckBoxItems);


       if($model->save())

           $this->redirect(blahblah);



or if you are planning to save the checkbox data to another new row, it would be a little longer, but basically it would have the same logic. Never tested it out yet, but you can try it. Shoot back a message if I’m totally wrong




$model = new Post();


if(isset($_POST['Post']))

   $model->attributes = $_POST['Post'];


   $valid = $model->validate();    //<-- I'm just guessing this is how you validate >.<


   if($valid)

       $tempCount = 0; // needed to ensure the correct index

       $tempData = "";


       for($i=0;$i<count($model->yourCheckBox);$i++)

       {

          if(isset($model->yourCheckBox[$i]))

          {

             $tempData[$tempCount] = $model->yourCheckBox[$i];

          }

       }


       for($i=0;$i<count($tempData);$i++)

       {

          $model->setIsNewRecord(true);  // To make sure that this would add new record and not update

          $model->yourCheckBox = $tempData[$i];


          $model->save();

       }


       $this->redirect(blahblah);



I do think that there are better ways to do it, and I haven’t yet tried out this:

http://www.yiiframework.com/wiki/81/

It shows how to delete multiple records, wherein the data to be deleted came from some few check boxes. I think that this could also be used in saving items, I’m not sure though since I never had got the opportunity to try this out, correct me if I’m wrong.

Please follow this articles -

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

[twitter]rohisuthar[/twitter]

Uninitialized string offset: 0

its displays this abv error, please help me! can i need to attach the code? somebody help?

hi,

I am getting a error while select multiple check box and insert into mysql table ,Please somebody Help

[u][i][b]CDbException

Column name must be either a string or an array.[/b][/i][/u]

<---------------------------------- This is the error

D:\xampp\htdocs\emerald_erp\framework\db\schema\CDbCommandBuilder.php(828)

816 }

817 if(count($values)===1)

818 {

819 $entries=array();

820 foreach($values[0] as $name=>$value)

821 $entries[]=$prefix.$table->columns[$name]->rawName.($value===null?’ IS NULL’:’=’.$value);

822 return implode(’ AND ',$entries);

823 }

824

825 return $this->createCompositeInCondition($table,$values,$prefix);

826 }

827 else

828 throw new CDbException(Yii::t(‘yii’,‘Column name must be either a string or an array.’));

829 }

830

831 /**

832 * Generates the expression for selecting rows with specified composite key values.

833 * @param CDbTableSchema $table the table schema

834 * @param array $values list of primary key values to be selected within

835 * @param string $prefix column prefix (ended with dot)

836 * @return string the expression for selection

837 */

838 protected function createCompositeInCondition($table,$values,$prefix)

839 {

840 $keyNames=array();

/************* THis is FUNCTION of update query*****************/////

public function actionUpdate($id)

{


	&#036;model=&#036;this-&gt;loadModel(&#036;id);


	&#036;menumodel = new MenuRoles;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['Roles']))


	{


	


		&#036;model-&gt;attributes=&#036;_POST['Roles'];


		//&#036;roleid = &#036;model-&gt;id;


	


		if(&#036;model-&gt;save()) {


			&#036;menumodel-&gt;attributes=&#036;_POST['MenuRoles'];


			


		foreach(&#036;menumodel-&gt;menu_id as &#036;menuid){


			


				&#036;menumodel-&gt;menu_id = &#036;menuid;


				&#036;menumodel-&gt;role_id = 1;


				&#036;menumodel-&gt;save();


			}


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


			}


	}


			&#036;this-&gt;render('update',array(


		'model'=&gt;&#036;model,


		'menu'=&gt;&#036;menumodel,


	));

/

}

Same thing in check boxlist.

I want in update case .

Do anyone have idea for it?

This post was helpful

Thanks