Hi,
I have models called Monitor, Channel and a connection model called ChannelMonitor.
Monitor and Channel have an n:n relationship.
I now have tried to add a CGridView containing the ChannelMonitor model entries in the system.
The problem is that when the CGridView is added to the monitor update page the buttons related to every row in the table have links looking like this /index.php?r=monitor/view&id=2 instead of /index.php?r=channelMonitor/view&id=2.
Action in MonitorController:
public function actionUpdate()
{
$model=$this->loadModel();
//Get the selected channels
$channelMonitorDataProvider=new CActiveDataProvider('ChannelMonitor');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Monitor']))
{
$model->attributes=$_POST['Monitor'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model, 'channelMonitorDataProvider'=>$channelMonitorDataProvider
));
}
Code in the view:
<?php
$this->breadcrumbs=array(
'Monitors'=>array('index'),
$model->name=>array('view','id'=>$model->id),
'Update',
);
$this->menu=array(
array('label'=>'List Monitor', 'url'=>array('index')),
array('label'=>'Create Monitor', 'url'=>array('create')),
array('label'=>'View Monitor', 'url'=>array('view', 'id'=>$model->id)),
array('label'=>'Manage Monitor', 'url'=>array('admin')),
);
?>
<h1>Update Monitor <?php echo $model->id; ?></h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'monitor-channel-grid',
'dataProvider'=>$channelMonitorDataProvider,
'columns'=>array(
'channel.id',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
I have tried loading the channels in to the CgridView as well and it still uses monitor as the link to edit.
Is this a bug?
I know I can override it using something like this(but it doesn’t seem right that I have to do that):
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'monitor-channel-grid',
'dataProvider'=>$channelMonitorDataProvider,
'columns'=>array(
'channel.id',
array(
'class'=>'CButtonColumn',
'updateButtonUrl'=>'Yii::app()->createUrl("channelmonitor/update", array("id"=>$data->id))',
),
),
)); ?>
I really think that the CGridView should use the urls for the model that is selected in the CDataProvider and not use the main model urls.
I have tried this in the latest version of the trunk.
Thanks
/John