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
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']))
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']))
// 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.
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));
}