Dear Sanjay
I hope the following is helpful in your scenario.
To simulate your scenario, I have created three tables.
1.user (Model:User)
2.customer (Model:Customer)
3.user_customer (Model:UserCustomer)
Short Summary of what I have done.
[color="#000080"]1.Created a table user_customer with columns user_id and cust_id.It was left empty.
user_id refers to user.id and cust_id refers to customer.id.
-
admin.php in /views/userCustomer will contain following elements.
1.user gridview with checkbox column.
2.customer gridview with checkbox column.
3.form to create UserCustomer.
4.userCustomer gridview.
-
When admin checks a row in usergrid, user_id is dynamically updated in the form.
Sameway when admin checks rows in customer-grid, customer field is dynamically updated in the form.
-
When admin submits the form, userCustomer-grid is dynamiclly updated with new values.
-
Modified the action userCustomer/index in such a way that it displays only customers associated with particular
user.
6.Modified loginUrl to redirect the user to userCustomer/index.
[/color]
[color="#2F4F4F"]Kindly go through the following code blocks.[/color]
UserCustomer.php
<?php
class UserCustomer extends CActiveRecord
{ public $customers;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'user_customer';
}
public function rules()
{
return array(
array('user_id, cust_id', 'required'),
array('cust_id', 'unique'),
array('user_id, cust_id', 'numerical', 'integerOnly'=>true),
array('id,user_id, cust_id,customers', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'customer' => array(self::BELONGS_TO, 'Customer', 'cust_id'),
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'user_id' => 'User',
'cust_id' => 'Customer',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->with=array('user','customer');
$criteria->compare('id',$this->id);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('cust_id',$this->cust_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array('attributes'=>array(
'user_id'=>array('asc'=>'user.username','desc'=>'user.username DESC'),
'cust_id'=>array('asc'=>'customer.name','desc'=>'customer.name DESC'),'*'
)),
));
}
}
UserCustomerController.php
public function actionCreate()
{
if(Yii::app()->request->getIsAjaxRequest()&&isset($_POST['UserCustomer']))
{
$user=$_POST['UserCustomer']['user_id'];
$customers=$_POST['UserCustomer']['customers'];
$customers=explode(',',$customers);
foreach($customers as $customer)
{
$model=new UserCustomer;
$model->user_id=$user;
$model->cust_id=$customer;
if($model->validate())
$model->save(false); //already validated.
}
}
}
public function actionAdmin()
{
$user=new User('search');
$customer=new Customer('search');
$userCustomer=new UserCustomer('search');
$user->unsetAttributes();
$customer->unsetAttributes();
$userCustomer->unsetAttributes();
if(isset($_GET['User']))
$user->attributes=$_GET['User'];
if(isset($_GET['Customer']))
$customer->attributes=$_GET['Customer'];
if(isset($_GET['UserCustomer']))
$userCustomer->attributes=$_GET['UserCustomer'];
$this->render('admin',array(
'user'=>$user,
'customer'=>$customer,
'userCustomer'=>$userCustomer,
));
}
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('UserCustomer',array(
'criteria'=>array('condition'=>'user_id='.Yii::app()->user->id),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
views/userCustomer/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$user->search(),
'filter'=>$user,
'columns'=>array(
'id',
'username',
'password',
'email',
'status',
array(
'class'=>'CButtonColumn',
),
array(
'class'=>'CCheckBoxColumn',
'selectableRows'=>1 //allows to check only one row.
),
),
)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'customer-grid',
'dataProvider'=>$customer->search(),
'filter'=>$customer,
'columns'=>array(
'id',
'name',
array(
'class'=>'CButtonColumn',
),
array(
'class'=>'CCheckBoxColumn',
'selectableRows'=>2 //allows to check multiple rows.
),
),
)); ?>
<div class="wide form">
<?php $form=$this->beginWidget('CActiveForm'); ?>
<div class="row">
<?php echo $form->label($userCustomer,'user_id'); ?>
<?php echo $form->textField($userCustomer,'user_id',array('id'=>'user')); ?>
</div>
<div class="row">
<?php echo $form->label($userCustomer,'customers'); ?>
<?php echo $form->textField($userCustomer,'customers',array('id'=>'customers')); ?>
</div>
<div class="row buttons">
<?php echo CHtml::button('Save',array('id'=>'butt')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-customer-grid',
'dataProvider'=>$userCustomer->search(),
'filter'=>$userCustomer,
'columns'=>array(
'id',
array(
'name'=>'user_id',
'value'=>'$data->user->username'),
array(
'name'=>'cust_id',
'value'=>'$data->customer->name'),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
<?php
Yii::app()->clientScript->registerScript('getUser','
$("body").on("change","#user-grid input[type=\"checkbox\"]",function(){
var checked=$("#user-grid").yiiGridView("getChecked","user-grid_c6");
$("#user").val(checked);
});
');
Yii::app()->clientScript->registerScript('getCustomer','
$("body").on("change","#customer-grid input[type=\"checkbox\"]",function(){
var checked=$("#customer-grid").yiiGridView("getChecked","customer-grid_c3");
$("#customers").val(checked);
});
');
Yii::app()->clientScript->registerScript('assignCustomer','
$("body").on("click","#butt",function(){
$.ajax({
type:"POST",
url:"'.Yii::app()->createUrl('userCustomer/create').'",
data:$(".wide form").serialize(),
success:function(data){
$("#user-customer-grid").yiiGridView("update",{
data:{"UserCustomer[user_id]":$("#user").val()}});
}
});
return false;
});
');
In SiteController::actionLogin
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login())
$this->redirect(Yii::app()->createUrl('userCustomer/index'));
}
[color="#000080"][b]
In admin.php we have registered three scripts.[/b][/color]
-
Yii::app()->clientScript->registerScript('getUser','
$("body").on("change","#user-grid input[type=\"checkbox\"]",function(){
var checked=$("#user-grid").yiiGridView("getChecked","user-grid_c6");
$("#user").val(checked);
});
');
Here "user-grid_c6" indicates the checkbox column is the seventh column in the grid.
Whenever admin checks a row , textfield for the user_id is updated with row id.
-
Yii::app()->clientScript->registerScript('getCustomer','
$("body").on("change","#customer-grid input[type=\"checkbox\"]",function(){
var checked=$("#customer-grid").yiiGridView("getChecked","customer-grid_c3");
$("#customers").val(checked);
});
');
Here "customer-grid_c3" indicates the checkbox column is the fourth column in the grid.
Whenever admin checks a row or multiple rows , textfield for the customer is updated with all the row ids.
-
Yii::app()->clientScript->registerScript('assignCustomer','
$("body").on("click","#butt",function(){
$.ajax({
type:"POST",
url:"'.Yii::app()->createUrl('userCustomer/create').'",
data:$(".wide form").serialize(),
success:function(data){
$("#user-customer-grid").yiiGridView("update",{
data:{"UserCustomer[user_id]":$("#user").val()}});
}
});
return false;
});
');
This script creates records for UserCustomer model from the form, when it is submitted by AJAX request.
During the same time it dynamically updates the userCustomer-grid with available records in the table. for the particular user.
Finally what we have acheived?
On a same page, we can allot the customers for a particular user and delete a customer for particular user.
Thus we can finetune our assignment.
Regards.