Hello
I am new to yii and i am unable to store multiple selected data to database.
there are three tables in my database ‘product’ ‘seller’ and ‘productseller’ with many to many relationship. product table contain pro_name and pro_id fields, seller table has sel_id and sel_name fields and productseller table has pro_id and sel_id fielsd.
What i am doing is to choose seller name and related multiple products from from dropdown list and then store them in productseller table.
My view:
<div class="row">
<?php echo $form->labelEx($model,'Select Seller'); ?>
<?php echo $form->dropDownList($model,'sel_id', CHtml::listData(Seller::model()->findAll(), 'sel_id', 'sel_name')); ?>
<?php echo $form->error($model,'sel_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Choose Products'); ?>
<?php //echo $form->textField($model,'maincat_id'); ?>
<?php echo $form->dropDownList($model, 'pro_id', CHtml::listData(Product::model()->findAll(), 'pro_id', 'pro_name'),
array('multiple'=>'multiple', 'size'=>5)); ?>
<?php echo $form->error($model,'maincat_id'); ?>
</div>
My seller controller
$model = new ProductSeller;
$this->render('insertItems', array('model'=>$model));
if(isset($_POST['ProductSeller']))
{
$model->attributes=$_POST['ProductSeller'];
if($model->save())
$this->redirect(array('view','id'=>$model->sel_id));
$this->redirect(array('view','id'=>$model->pro_id));
}
Seller Model
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(
'productSellers' => array(self::HAS_MANY, 'ProductSeller', 'sel_id'),
);
}
Product Model
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(
'productSellers' => array(self::HAS_MANY, 'ProductSeller', 'pro_id'),
);
}
ProductSeller Model
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(
'pro' => array(self::BELONGS_TO, 'Product', 'pro_id'),
'sel' => array(self::BELONGS_TO, 'Seller', 'sel_id'),
);
}
What steps should i follow to save pro_id and sel_id to database?
Thanks