Save multiple input from drop down to database

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

You should save the info from only one of the controllers, you should initialize the three models in the action in the controller and so you should save the info. I put you a example. I hope this will serve.




public function actionCreate()

	{

		$model=new Cliente;

		$modelClienteRuta= new Clienteruta;

		$modelClientePublicacion = new Clientepublicacion;

		$modelClienteDemograf = new Demograficocliente;


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

		{			

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




				$modelClienteRuta->cliente_idcliente=$model->idcliente;	///Guarda la ruta entrega del cliente

				$modelClienteRuta->rutazona_idrutazona=$ruta;

				$modelClienteRuta->distintivo= 'r';

				$modelClienteRuta->save();


$modelClientePublicacion->cliente_idcliente=$model->idcliente;

						$modelClientePublicacion->tipopublicacion_idtipopublicacion=$publica;

						$modelClientePublicacion->save();




One more thing you should pass the model to the view create.

ProductSeller model handles only a single instance of Product-Seller relation. So you have to create a model (derived from CFormModel) that can handle multiple instances in your view form.




class PSInput extends CFormModel

{

...

   public $sel_id;     // seller id

   public $prod_ids;   // array of product ids

...



And decode it to multiple instances of ProductSeller in some controller action to save them.




...

    $model = new PSInput;

    if(isset($_POST['PSInput'])){

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

        foreach($model->prod_ids as $prod_id) {

            $spModel = new SellerProduct;

            $spModel->sel_id = $model->sel_id;

            $spModel->prod_id = $prod_id;

            $spModel->save();

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

        }

    } else {

        ...

    }

    $this->render('insertItems', array('model'=>$model));

...



Note that the code above is just for an illustration purpose. It may work for the first creation of the relations, but DOES NOT for updating for sure. But I hope you can start a first step from here.