Setting a custom key property for CArrayDataProvider? - yii

I have:
$dataProvider = new CArrayDataProvider ($items ,
array(
'pagination'=>array(
'pageSize'=> 10,
),
));
And I render:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=> $dataProvider,
'itemView' => 'items',
'summaryText' => '',
'emptyText' => '
',
));
Problem is it whines about the ID in the dataset. It wants it to be called "id". So I geT:
Undefined property: stdClass::$id
C:\wamp\www\yii\web\CArrayDataProvider.php(108)
How can I change it so that it uses "itemid" instead of "id" as the column reference?

Set the data provider's keyField property:
$dataProvider = new CArrayDataProvider ($items ,
array(
'keyField' => 'itemid',
'pagination'=>array(
'pageSize'=> 10,
),
));

Related

remove button from CGridView with condition

Hi I have CRUD generated CGridView in yii. I need to add a new button to CGridView rows and hide it if appointment_status(one of CGridView column) value equals 0
This is my code of CGridView,
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => 'CHtml::button("$data->appointment_status",array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$data->id))."\'"))',
'visible'=>$data->appointment_status==1,
),
array(
'class' => 'CButtonColumn',
),
),
));
But all I'm getting is error stating,
Undefined variable: data
It would be great help if someone can look into it.
you can use like this:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => function ($data) {
if ($data->appointment_status == 1) {
return CHtml::button("$data->appointment_status", array("onclick" => "document.location.href=\'" . Yii::app()->controller->createUrl("controller/action", array("id" => $data->id)) . "\'"));
} else {
return;
}
}
),
array(
'class' => 'CButtonColumn',
),
),
));
Your 'visible' handling the column visibility and not the button, you can use custom attribute on model to create and handle the button visibility.
add to your model:
public function getConfirmationButton()
{
if ($data->appointment_status == 1) {
return CHtml::button($this->appointment_status,array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$this->id))."\'"));
} else {
return '';
}
}
and call it in your view:
..........
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => '$data->confirmationButton',
),
...........
visible is a boolean or a PHP expression that will be evaluated to give a boolean. During the evaluation $data is assigned to the current item from the dataProvider used. $data doesn't exist outside of the evaluation function evaluateExpression(). As such the implementation should be:
`visible` => '$data->appointment_status == 1',
You need to quote value of visible key in link array. So instead of this:
'visible'=>$data->appointment_status==1
It should be:
'visible'=>'$data->appointment_status==1'
it should work now.
You will get undefined variable because visible not allow any callback.
Try this solution, it's yii2 code and i don't know much of Yii.
'delete' => function ($url, $model) {
return ($model->isVisible($model)) ?
Html::a('<span class="glyphicon glyphicon-trash"></span>',
$url,
['title' => Yii::t('app', 'Delete')]) : '';
public static function isVisible($data)
{
return ($data->appointment_status == 1) ? true : false;
}

Yii cgridview cactivedataprovider doesnt work properly

I configured the Yii cactivedataprovider as the documentation writes:
$criteria = new CDbCriteria();
$criteria->together = true;
$criteria->with = array(
'relationId0',
'relationId1',
...
);
$criteria->compare('"relationId0".property0', $this->relationId0_property0, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'attributes' => array(
...
'relationId0.property0' => array(
'asc' => '"relationId0".property0',
'desc' => '"relationId0".property0 DESC',
),
...
)
)
));
so, when the ->together is false, then the gridview works properly and gets all rows what the pagination allowed, but in this case the compare (so the search) doesnt work (because this way doesnt use the related objects in the sql query),
but when ->together is true (and it is the solution supposedly) the compare is working but the gridview gets random number of rows in each page.
Thank you for helping.
Try to add pagination into your dataprovider.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination'=>array('pageSize'=>10),
'sort' => array(
'attributes' => array(
...
'relationId0.property0' => array(
'asc' => '"relationId0".property0',
'desc' => '"relationId0".property0 DESC',
),
...
)
)
));
This should solve your problem.

How to add radio into cgridview

I want to add single radio button to gridview row by row at yii. I googling like this "yii single radio button into cgridview" but I can't get what I want.
This is my gridview
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_implementerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'implementers[]',
),
'value'=>'$data->id',
'checked'=>function($data, $row) use ($current_implementers){
return in_array($data->id, $current_implementers);
}
),
array(
'name'=>'lead_implementer',
'type'=>'raw',
'value' => 'CHtml::radioButtonList("lead_implementer","'. $lead_implementer .'",array($data->id), array("template" => "{input}", "style" => "width:20px!important", "separator"=>" "))',
),
'username',
),
)); ?>
How can I do?
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_implementerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'implementers[]',
),
'value'=>'$data->id',
'checked'=>function($data, $row) use ($current_implementers){
return in_array($data->id, $current_implementers);
}
),
array(
'name'=>'lead_implementer',
'type'=>'raw',
'value' => 'CHtml::radioButtonList("lead_implementer","'. $lead_implementer .'",array("$data->id"=>"$data->id"), array("template" => "{input}", "style" => "width:20px!important", "separator"=>" "))',
),
'username',
),
)); ?>

Yii ClistView pagination not working

My Clistview Pagination is not working properly.I tried every thing but still not getting success.Here is my model function:
public function getallone()
{
$criteria = new CDbCriteria;
$criteria->select = "t.id";
$criteria->condition = "t.featured=1 AND t.status = 1";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array('pageSize' => 25),
));
}
Am still getting only 10 items per page but i want 25.I tried to 2 items but its also not working. Whats wrong with that?Didnt find.
And this is my view code:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->getallone(),
'itemView'=>'_allone',
'enablePagination' => true,
)); ?>
You shoud try this for better pagination option
$condition='type=:type';
$dataArray[':type']=$type;
$dataProvider=new CActiveDataProvider('modelname', array(
'criteria'=>array(
'select'=>'*',
'condition'=>$condition,
'params'=>$dataArray,
'order'=>'id DESC',
'offset'=>($page-1)*$limit,
'limit'=>$limit,
),
'pagination'=>array(
'pageSize'=>$limit,
),
));
$totalActivity= $dataProvider->getTotalItemCount();
http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider for reference
try and use CArrayDataProvider
here is how do it:
public function actionIndex() {
$command = Yii::app()->db->createCommand("
SELECT u.id as u_id, u.link as u_link, u.title as u_title, u.description as u_description, u.public as u_public, u.created as u_created, u.status as u_status, w.id as w_id, w.domain as w_domain, w.status as w_status
from url as u
left join website as w
on w.id = u.website_id
where u.title!='null' and u.status = 1 and w.status = 1 and u.public = 1;
");
$news = $command->queryAll();
$dataProvider = new CArrayDataProvider($news, array(
'id' => 'user',
'keyField' => 'u_id',
'sort' => array(
'defaultOrder' => 'u_id desc',
),
'pagination' => array(
'pageSize' => 10,
),
));
$model_website = Website::model()->findAllByAttributes(array('status' => 1), array('limit' => 30, 'order' => 'id desc'));
$this->render('index', array(
'model_website' => $model_website,
'dataProvider' => $dataProvider,
));
}
and the clistview:
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'template' => "{summary}\n{pager}\n{items}\n{summary}\n{pager}",
'itemView' => '_index',
'pager' => array(
'maxButtonCount' => 10,
),
)
);

How to add custom column in CGridView in YII?

I need to add a column in CGridView.
I use this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'pager' => array(
'firstPageLabel' => '<<',
),
'columns'=>array(
'username',
'name',
'email',
'creationDate',
array(
'class' => 'CButtonColumn',
'template' => '{change} {view}',
'buttons' => array(
'change' => array(
'url'=> "'http://test.com/userservice/".$model->username."'",
),
),
),
array(
'name' => 'test',
'value' => 'testtest',
)
),
));
But I got error:
Property "User.test" is not defined.
You're almost there, in your column array you would use the name param for attributes of the model in the dataprovider, instead for custom columns you can use header like so:
'columns'=>array(
...
array(
'header' => 'test',
'value' => '"testtest"',
),
...
)
You can write normal code on CGridView like this .
'columns'=>array(
'username',
'name',
'email',
'creationDate',
'test',
---
),
If you put code like this on your respective model .
---
public $test ;
public function afterFind() {
$this->test = 'Test Variable' ; // put your custom code to reflect exact need
return parent::afterFind();
}
---