yii CButtonColumn in gride view gives error

104 Views Asked by At

i have a admin page with CGrideView , but when i want to change my button column to add some other buttons gives this error : CButtonColumn and its behaviors do not have a method or closure named "getId".

admin action :

    public function actionAdmin()
    {
        $model=new Block('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Block'])) {
            $model->attributes=$_GET['Block'];
        }

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

admin view :

 $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'block-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'name',
        'content',
        'type',
        'enable',
        array(
            'class'=>'CButtonColumn',
            'template' => '{view}{update}',
            'buttons' => array(
                'update' => array(
                    'url' => 'Yii::app()->controller->createUrl("update", array("name"=>$data->name))'
                ),
                'view' => array(
                    'url'=>'CController::createUrl("view", array("name"=>$data->name))'
                ),
            ),
        ),
)));
2

There are 2 best solutions below

2
hamed On

The order of elements in template must be equal to the order of element in buttons. You have {view}{update} as the template, but you have defined update button first! So I think changing 'template'=>'{view}{update}' into 'template'=>'{update}{view}' probably can solve your problem.

2
abbas hoseini On

solved! the reason is in the :

        'view'=>array(
         'url'=>'CController::createUrl("view",array("name"=>$data->name))'
        ),

it should be :

                'view'=>array(
                'url'=>'Yii::app()->controller->createUrl("view", array("name"=>$data->name))'
            ),

and why ? because (): Because Yii::app()->controller it is instance Controller of current application. Controller have property private $_id. CController::createUrl it is just static method. In method createUrl() calls method $this->getId(), but when you call static method instance is not created-@DanilaGanchar .

so in CController::createUrl it can't find the id of controller and for use that i should give it argument like this CController::createUrl("/page/view",array("name"=>$data->name)) i try that now and worked