Reputation: 663
Having some trouble with CGridView on Yii Framework...
I'm looking to replace the contents of a column based on the value it holds.
I need to handle special cases so I added a function into the model to return a value to the GridView.
I get the resulting error " Undefined variable: model
".
I'm sure it's likely something simple. Is it because my dataProvider is not model?
Here is a shortened version of my code:
<?php
/* @var $this BookController */
/* @var $dataProvider CActiveDataProvider */
/* @var $model Book */
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'book-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'userName',
'header'=>'Name',
),
array(
'name'=>'status',
'header'=>'Status',
'type'=>'raw',
'value'=>array($model, 'statusText')
),
)
));
?>
And here is code in models/Book.php
class Book extends CActiveRecord
{
...
...
public function statusText($data, $row) {
$content = '';
if (CHtml::encode($data->status) == "processed") {
$content = "Process completed";
}
else if ($data->status=="") {
$content = "Queued for Processing";
}
else {
$content = CHtml::encode($data->status);
}
return $content;
}
...
...
}
Upvotes: 0
Views: 770
Reputation: 3893
Here is a simplified example from my current project;
<?php
//My controller
class NewsController extends CController {
//The admin action
public function actionAdmin() {
$model = new News;
$this->render('admin', array(
'model' => $model
));
}
}
//In my view file
$this->widget('ext.widgets.MyTbGridView', array(
'dataProvider' => $model->search(),
'columns' => array(
array(
'name' => 'id',
'filter' => false,
),
array(
'name' => 'title',
),
array(
'value' => array($model, 'gridDate')
),
),
));
//My model function
class News extends CActiveRecord {
public function gridDate($data, $row) {
return 'Date formatted!';
}
}
?>
The code 'value' => array($model, 'gridFormatDate'),
is important. there are two possibilities here. The function can reside in the controller, in which case it should be 'value' => array($this, 'gridFormatDate'),
or it can be in the model, in which case the correct code is given
Upvotes: 1
Reputation: 1914
In stead of array($model, 'statusText')
, try '$data->statusText'
.
The method in your model should be like this:
public function getStatusText() {
$content = '';
if (CHtml::encode($this->status) == "processed") {
$content = "Process completed";
}
else if ($this->status=="") {
$content = "Queued for Processing";
}
else {
$content = CHtml::encode($this->status);
}
return $content;
}
Upvotes: 0