Insert Many Id

Hi, I’m trying to create a communication system in my app. I have form with title,content, signature and adress_user_id that will read msg. Now I have problem cuz i don’t know what to do if I want to choose many users.

I want to use http://www.yiiframework.com/extension/echmultiselect/ extension. Maybe you have better way… I want something like list of checkboxes with users, select and send. I thought about using http://www.yiiframework.com/extension/multimodelform/ but it creates needed fields only for 1 user. So 10 msg is 10*3 needed fields. (pointless)

Now I only have possibility of choosing 1 user, how should I do this ?

form:


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'adress-form',

	'enableAjaxValidation'=>false,

)); ?>

        

	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'title'); ?>

		<?php echo $form->textField($model,'title',array('size'=>45,'maxlength'=>45)); ?>

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'content'); ?>

		<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>

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

	</div>


	


	<div class="row">

		<?php echo $form->labelEx($model,'signature'); ?>

		<?php echo $form->textField($model,'signature',array('size'=>45,'maxlength'=>45)); ?>

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

	</div>

        <!--

	<div class="row">

		<?php //echo $form->labelEx($model,'adress_user_id'); ?>

		<?php //echo $form->dropDownList($model,'adress_user_id',$model->getAllUser()); ?>

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

	</div> -->

        <div class="row">

        <?php

        $data= CHtml::listData(User::model()->findAll(), 'id', 'firstname');

        $this->widget('ext.EchMultiSelect.EchMultiSelect', array(

            'model' => $model,

            'dropDownAttribute' => 'adress_user_id',     

            'data' => $data,

            'dropDownHtmlOptions'=> array(

                'style'=>'width:378px;',

            ),

        )); ?>

        </div>

	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

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(

			'adressUser' => array(self::BELONGS_TO, 'User', 'adress_user_id'),

		);

	}

Controller:


public function actionCreate()

	{

		$model=new Adress;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

                        if($model->adress_user_id!=='')

                            $model->adres_users_id=implode(',',$_POST['Adress']['adress_user_id']);

			if($model->save())

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

		}

                $model->adress_user_id=explode(',', $model->adress_user_id);

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

			'model'=>$model,

		));

	}


public function actionIndex()

	{

                $current_id = Yii::app()->user->getId();

		$dataProvider=new CActiveDataProvider('Adress', array(

                    'criteria'=>array(

                        'condition'=>'adress_user_id='.$current_id,

                        'order'=>'create_date DESC',

                    )

                ));

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

			'dataProvider'=>$dataProvider,

		));

	}

implode/explode will not work for my id INT column, how should I do it ?

Still no idea ? I was looking for it for whole day but still nothing ;/

Hi

serialize your array while inserting into the db





$value =serialize($_POST['Adress']['adress_user_id'])



Store the this in db

and to retreive from db




 unserialize($value ) // Value from the db , this will return array.

 



Yea I think that I don’t know where should i put it, or maybe whole idea is wrong. It should be in model in beforeSave function or in controller actionCreate ??

I have only field with user_id(INT) from related model, maybe I should create another field(VARCHAR) to store all this Id and then retreive it and somehow use them to point proper column ? Will it work ?

In general you need to create a method in your Controller class and should call it to save this data from your create action.Since if you are thinking to do this code in beforeSave then you can face some issue.So, in a ideal way should create a Private Method in Controller class. Or better then this …You can define a method in a Behavior class and attach that behavior to your controller class.