yii CButtonColumn in gride view gives error - php

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))'
),
),
),
)));

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

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.

Related

Yii - Pass model ID into a view from CButtonColumn image

Alright, still new to Yii I am attempting to create a button in a column of CGridView. This button, when clicked, will take the user to the "view.php" page and display the information of the node they clicked on by passing in its ID.
I am frustrated that I cannot figure out how to simply add a link to the image that will direct my users. Here are some snippets of the code I have been working on for the past couple of days.
index.php
<?php echo CHtml::beginForm(); ?>
<?php
$pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'nodes-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'class' => 'CButtonColumn',
'template' => '{restart}',
'buttons' => array
(
'restart' => array
(
'label'=>'Restart this node',
'imageUrl'=>Yii::app()->baseUrl.'/images/refresh.png',
'options'=>array('id'=>'NB_bounce_Button'),
'url'=>array('view', 'id'=>$model->id),
)
),
),
/* 'id', */
'name',
'url',
'description',
'node_type',
'last_bounced',
//..
NodeBouncerController.php (View action)
/**
* Displays a particular model.
* #param integer $id the ID of the model to be displayed
*/
public function actionView($id) {
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
The button is on the left. (Refresh green arrow)
What am I doing wrong? More specifically, am I using the buttons of CButtonColumn incorrectly? If so, how may I fix this?
My company uses: Yii v1.1.8.r3324
EDIT: [8/10/15]
It seems I may have been overcomplicating it. What I needed was to simply have an image-link that when clicked went to the view for that particular node that was clicked. Well, Gii auto-generates the particular view I needed and the code associated with it (as seen above).
My fix was to do away with the over complicated mess that I had and keep it simple. Use the template already provided for "view" and just alter it to my needs.
Like so:
/* 'id', */
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'buttons' => array
(
'view' => array
(
'label'=>'Restart this node',
'imageUrl'=>Yii::app()->baseUrl.'/images/refresh.png',
'options'=>array
(
'id'=>'NB_bounce_Button'
)
)
),
),
'name',
'url',
'description',
'node_type',
'last_bounced',
//....
It would be nice to know how to do this manually but I needed something to simply work for now and expand on it later for work.
I think the problem is url of the button. Because you did not include controllerID in the url. You should use this:
'url' => 'Yii::app()->createUrl("nodeBouncer/view", "id"=>$model->id)';
For creating url in yii, usually you should follow this pattern:
contollerID/actionID

Yii CGridView renders a new page instead of AJAX

In the index page of my site I have floating div-block with CGridView inside.
I need to use sort, filter, paging etc. options, but only via updating this only div and not refreshing the whole page.
Data rendering as it should, i'm stuck only with updating grid contents - all <a>'s have href, that sending user to specified view.
Here's the view for grid: (it's the only content of my views.users.usersGrid.php)
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'ajaxUrl' => 'modules/subcntrl/usersGrid',
'ajaxUpdate' => 'users-grid',
'columns' => array(
array(
'name' => 'name',
'type' => 'raw',
'value' => 'CHtml::encode($data->name)'
),
array(
'class'=>'CButtonColumn',
),
),
));
It's called from views.users.users.php: <?php $this->actionUsersGrid(); ?>
Controller:
public function actionUsers() {
$this->renderPartial('users');
}
public function actionUsersGrid() {
if(!Yii::app()->request->isAjaxRequest) die('Url should be requested via ajax only');
$model = new Users();
$this->renderPartial('usersGrid',array(
'model' => $model,
));
}
Would appreciate any help
please use the third and fouth parameter in renderPartial method like this,
$this->renderPartial('users',array(),false,true);
it will solve your problem (setting the processOutput=true ) in the fourth parameter
please use the third and fouth parameter in renderPartial method like this,
$this->renderPartial('users',array(),false,true);
it will solve your problem (setting the processOutput=true ) in the fourth parameter
this is not working in my code. i am using like this in ma controler
$this->renderPartial('_searchQuote', array('model' => $model, 'month' => $month, 'user' => $user), false, $processOutput=false );

How to get controller name inside CGridView?

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"]))',
),
),
));
?>
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.
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

How to convert model data objects array to dataProvider

Suppose I have model User which have many to many relation to itself named as friends.
so $user->friends (or $model->friends in view) gives me an array of User objects. I wanted to display the friends as gridview. But CGridView data as dataProvider object. Googling for it found the way to convert array of model objects to dataProvider object as given below.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gridUser',
'dataProvider' => new CArrayDataProvider($model->friends, array()),
));
Now using this I get an error
Property "User.id" is not defined.
UPDATE
public function relations()
{
return array(
'friends' => array(self::MANY_MANY, 'User', 'friendship(user_id, friend_id)'),
);
}
I use two stage building the provider shown below. But I found that it gives you trouble in terms of Pagination. I have not bothered to resolve that problem since am doing other things
$dataProvider = new CArrayDataProvider('User');
$dataProvider->setData($model->friends);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gridUser',
'dataProvider' =>$dataProvider,
));
That being said, your code should work (see the example below from API docs). I suspect there is wrong attribute in your relations than the provided code. Re-check the relation definition if it is ok
From Yii docs:
$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
// or using: $rawData=User::model()->findAll(); <--this better represents your question
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'username', 'email',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
Got it :) , i can use CActiveDataProvider instead of CArrayDataProvider as given below
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gridUser',
'dataProvider' => new CActiveDataProvider('User', array(
'data'=>$model->friends,
)),
//...... columns display list.....
));
Anyways thanks for the reply #Stefano

renderPartial in CGridView cell

I want to render a php view file in the cell of a CGridView.
Therefore I wrote this code:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'value'=>array($this, 'renderPartial("_lineProblems")')
))));
But I get this error:
BookController and its behaviors do not have a method or closure named "renderPartial('_lineProblems' )".
The stack trace shows this as the problem:
call_user_func_array(array(BookController, "renderPartial('_lineProblems' )"), array("data" => line, "row" => 0, 0 => CDataColumn))
I don't understand the error. $this is an instance of BookController and BookController is a child of CController.
Found a solution with the help of another Yii programmer
$controller = $this;
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'errors',
'value'=>function($data, $row) use ($controller){
return $controller->renderPartial('_lineProblems', array('errors'=>$data->errors), true);
}
)
),
));

Categories