0

In views controller name can be achieved by $this->id, but in CGridView $this->id evaluation results in another name (may be gridview widget name). (1) How can I get controller name in cgridview?

Also I tried to define a variable ($thisCtl = $this->id) before calling CGridview. Although $thisCtl has the controller name, in CGridview evaluation process it is not defined. (2) Is there any way to pass a parameter to CGridView?

<?php 
$thisCtl = $this->id;
$data = $model->search(); 
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'id'=>'insurance-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        'id',
        'registeration_date',
        'modification_date',
        array(
            'htmlOptions' => array('nowrap'=>'nowrap'), 
            'class'=>'bootstrap.widgets.TbButtonColumn', 
            'viewButtonUrl'=>'Yii::app()->createUrl("$thisCtl/view", array("id"=>$data["id"]))', 
            'updateButtonUrl'=>'Yii::app()->createUrl("$this->id/update", array("id"=>$data["id"]))', 
        ),
    ),
)); 

?>

Kai
  • 38,985
  • 14
  • 88
  • 103
hpaknia
  • 2,769
  • 4
  • 34
  • 63

2 Answers2

1

the problem is that $this in CGridView refers to it's widget. So we should use a global function to get current controller name:

Yii::app()->getController()->getId();

the code should look like:

<?php 
$thisCtl = $this->id;
$data = $model->search(); 
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'id'=>'insurance-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        'id',
        'registeration_date',
        'modification_date',
        array(
            'htmlOptions' => array('nowrap'=>'nowrap'), 
            'class'=>'bootstrap.widgets.TbButtonColumn', 
            'viewButtonUrl'=>'Yii::app()->createUrl("'.Yii::app()->getController()->getId().'/view", array("id"=>$data["id"]))', 
            'updateButtonUrl'=>'Yii::app()->createUrl("'.Yii::app()->getController()->getId().'/update", array("id"=>$data["id"]))', 
        ),
    ),
)); 

?>

Thanks to this post Getting Current Controller ID in Yii

Community
  • 1
  • 1
hpaknia
  • 2,769
  • 4
  • 34
  • 63
1

Since it appears you wish to use the controller value within the column values, you can do the following to get the controller name:

$this->grid->controller->id

As you are within a TbButtonColumn, you can access the grid property (since TbButtonColumn extends from CButtonColumn which in turn extends from CGridColumn) to access the TbExtendsGridView (which ultimately extends from CGridView) where you in turn access the controller property of the grid (which is defined by the widget parent), and finally the get id of the controller.

Then, assuming all your other code is correct, you would specify your gridview like so:

<?php 
$data = $model->search(); 
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'id'=>'insurance-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        'id',
        'registeration_date',
        'modification_date',
        array(
            'htmlOptions' => array('nowrap'=>'nowrap'), 
            'class'=>'bootstrap.widgets.TbButtonColumn', 
            'viewButtonUrl'=>'Yii::app()->createUrl($this->grid->controller->id."/view", array("id"=>$data["id"]))', 
            'updateButtonUrl'=>'Yii::app()->createUrl($this->grid->controller->id."/update", array("id"=>$data["id"]))', 
        ),
    ),
));

I haven't used any of the Tb extensions myself, by so long as they did not alter the built in behavior of the objects they extended, the above should work as desired.

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24
  • I tried your solution: Recoverable error Object of class TbExtendedGridView could not be converted to string – hpaknia Apr 21 '13 at 15:44
  • Hmm, it may have been an issue with the object chaining within the eval'd double quotes. Using `eval()` can be tricky sometimes. See if it works with the edit I just made. – Willem Renzema Apr 21 '13 at 16:18
  • it's OK, 1- what was your edit on your answer? 2- I prefer your answer more, because it's correct way of getting controller name from gridView while my answer is a remedy! – hpaknia Apr 21 '13 at 18:06
  • @HPM I changed `->createUrl("$this->grid->controller->id/update",` to `->createUrl($this->grid->controller->id."/update",)` for both the urls. Chaining objects within double quotes is what I suspect the problem was. – Willem Renzema Apr 21 '13 at 21:18