Multiselect Values How To Save In Database In Controller

i have using dropdown multiselect values save in db how to save ,i tryed in my controller side but its show error is "Array to string conversion" how to solve and my code for controller and view part dropdown

in my form

<div class="row">


<b style=" padding-right: 60px;">Targeted_muscles <span class="required">* </span> </b><?php echo $form->dropdownList($model,'targeted_muscles', CHtml::listData(Muscles::model()->findAll(array('order' => 'muscles_name')), 'id', 'muscles_name'), array('empty'=>'Select ' ,'multiple'=>'multiple',),array('class'=>'select','asc'=>'muscles_name', 


        )               ) 


            


           


            ?> 





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


</div>

in my controller

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


	{


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

foreach($_POST[‘ExMusclesDetails’][‘targeted_muscles’] as $value){

// OR 1,2

$ID_STRING= explode(",",$value);

$model->targeted_muscles=$value;

$model->save();

}

please help what is wrong in my code

iam multiple select option based only one values inserted at a time in database , how to save dropdown multiple values in my db any my controller code is

public function actionCreate()

{      


	$model=new ExMusclesDetails;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);


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

{

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


             


              $target =$_POST['ExMusclesDetails']['targeted_muscles'];


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


                           {


$model->targeted_muscles=$target[$i];


                           }





                           $model->save();   


			$this->redirect(array('view','id'=>$model->id));	


                            }


 


	 $this->render('create',array(


		'model'=>$model,


	));





}

what is the wrong in my code please help me

Are you trying to save these multiselect value in the same table or separate table. ??

If you are trying to save all this value in same table field then you can do something like this




$ID_STRING= explode(",",$_POST['ExMusclesDetails']['targeted_muscles'] );


$model->targeted_muscles=$ID_STRING;

$model->save();



And make sure that your field "targeted_muscles" in table is VARCHAR or TEXT type.Also it is defined in the model class rules section.

Then for sure It will save the value.

And if you are saving this Multiselect values in Different table then.

Create a multiple instance of model to save a record in Loop.Something like this




   // save model A

    $model_a->save();


    // save related models

    foreach($model_bs as $model_b ) {

        $model_b->model_a_id = $model_a->id; // set the foreign key

        $model_b->save();

    }



And Please from next type try to put your code in code block from editor. ;)

in my table targeted_muscles is integer field ,in this field i want to store all selected value by user in database but now its store only last selected value in database how to save other selected values

in my model code

public function relations()

{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'exname0' => array(self::BELONGS_TO, 'ExNameDetails', 'exname'),


		


		


                


                     'targetedMuscles' => array(self::HAS_MANY, 'Muscles', 'targeted_muscles'),


		'additionalMuscles' => array(self::HAS_MANY, 'Muscles', 'additional_muscles'),


                


                );


}





/**


 * @return array customized attribute labels (name=>label)


 */


public function attributeLabels()


{


	return array(


		'id' => 'ID',


		'exname' => 'Exercise Name',


		'targeted_muscles' => 'Targeted Muscles',


		'additional_muscles' => 'Additional Muscles',


	);


}

and my controller code action create

public function actionCreate()

{      


	$model=new ExMusclesDetails;


	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);


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

{

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


             


              $target =$_POST['ExMusclesDetails']['targeted_muscles'];


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


                           {


$model->targeted_muscles=$target[$i];


                           }





                           $model->save();   


			$this->redirect(array('view','id'=>$model->id));	


                            }


	 $this->render('create',array(


		'model'=>$model,


	));





}

what is wrong in my code plse

Please Read this carefully




   // save model A

    $model_a->save();


    // save related models

    foreach($model_bs as $model_b ) {

        $model_b->model_a_id = $model_a->id; // set the foreign key

        $model_b->save();

    }



You will get the answer.

$model_a = your parent model

$model_b = your child model for which you are saving this multiple values.

my database two table

Muscles

Exmusclesdetails

i want to save all values in exmusclesdetails ’

pls tell how to call in controller side

Could you please show your complete code for this action.

As per my understanding you are doing something like this.





$model_a =new ExMusclesDetails;


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

{


$model_a->attributes=$_POST['ExMusclesDetails'];


$model_a->save(); //It will save your ExMusclesDetails model attributes


$targets =$_POST['ExMusclesDetails']['targeted_muscles'];


foreach(targets as $target){

  $model_b = new Muscles();

  $model_b->attributes = $target; //Here $target will have attributes for your Muscles model class .Make sure you have assigned all required  attibutes for this model to save data.

  $model_b->save();

  $model_b->isPrimaryKey =null

  

}


$this->redirect(array('view','id'=>$model->id));

}






I think its a clear picture for you now.