Yii cgridview cactivedataprovider doesnt work properly - yii

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.

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 not showing records of a certain attribute

So I have created records and each has their own particular Service field. They are all filled up (not null).
In the MySQL database, there are also records of that field Service
But in my CGridView, my Service field is all blank. All the Service fields are blank for all the records. When I click on the button View - The one with the magnifying glass, it says Service - Not Set
Can I know what may cause the problem? Please help me Thanks!
UPDATED WITH CGRIDVIEW CODE
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'booking-grid',
'htmlOptions'=>array('class'=>'grid-view grid-size'),
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name'=>'date',
'htmlOptions'=>array('style'=>'text-align:center', 'height'=>'50px', 'width'=>'80px'),
'headerHtmlOptions'=>array('height'=>'30px'),
),
array(
'name'=>'timeStart',
'header'=>"Time",
'htmlOptions'=>array('style'=>'text-align:center', 'width'=>'80px'),
),
array(
'name'=>'client_id',
'value'=>'GxHtml::valueEx($data->client)',
'filter'=>GxHtml::listDataEx(Client::model()->findAllAttributes(null, true)),
'htmlOptions'=>array('width'=>'200px'),
),
array(
'name'=>'service_id',
'value'=>'GxHtml::valueEx($data->service)',
'filter'=>GxHtml::listDataEx(Service::model()->findAllAttributes(null, true)),
),
array(
'name' => 'complete',
'header'=>'Status',
'value' => '($data->complete == 0) ? Yii::t(\'app\', \'Open\') : Yii::t(\'app\', \'Close\')',
'filter' => array('0' => Yii::t('app', 'Open'), '1' => Yii::t('app', 'Close')),
'htmlOptions'=>array('width'=>'60px', 'style'=>'text-align:center'),
),
'remarks',
array(
'class' => 'CButtonColumn',
'visible'=>true,
'htmlOptions'=>array('width'=>'70px'),
'template' => '{view}{update}{delete}{changeComplete}',
'buttons'=>array(
'view' => array(
'visible'=>"true",
),
'update' => array(
'visible'=>"true",
),
'delete' => array(
'visible'=>"UserIdentity::isRoleMember('Admin')",
),
'changeComplete' => array(
'visible' => '$data->complete=="0"',
'imageUrl' => 'images/complete.png',
'options' => array(
'title' => 'Complete',
),
'url' => 'Yii::app()->createUrl("booking/changeComplete", array("id"=>$data->id))',
'click' => "function(){
$.fn.yiiGridView.update(\"booking-grid\", {
type:'POST',
url:$(this).attr('href'),
success:function(data) {
$.fn.yiiGridView.update(\"booking-grid\");
}
})
return false;
}",
),
),
),
))); ?>
I have tried looking at other CGridViews with the same field, they look the same and they're working perfectly fine.
I think the problem is $data->service. Probably your data has a relation with the Service entity. So for accessing attributes of Service you should use $data->service->attributeName.

filter grideview work with having condition in search function in criteria?

i have a grideview by this code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'lecture-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header'=>'name',
'type' => 'raw',
// 'name'=>'name',
'value' => 'CHtml::link($data->name,Yii::app()->baseUrl . "/uploads/" . $data->name)',
),
array(
'header'=>'pages',
'value'=>'$data->slide_num'
),
array(
'header'=>'type',
'value'=>'$data->type'
),
array(
'header'=>'Size',
'value'=>'$data->size'
),
// 'subject.name'
array (
'header'=>'subject',
'value' => 'ucfirst($data->subject->name)',
'filter' => CHtml::dropDownList('Lecture[subject_id]',
$model->subject_id, Chtml::ListData(Subject::model()->findAll('department_id='.$department_id),'id','name'),
array('empty' => '(Select)'))
)
/* array(
'name'=>'subject',
'type'=>'raw',
'value'=>"Subject::model()->find('id=2')->name",
'filter'=>''
),*/
),
));
and in model the search function is
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('slide_num',$this->slide_num,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('size',$this->size,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('subject_id',$this->subject_id);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
/*return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));*/
}
every thing is ok my target is filter that works in this search function correctly but i want to have a condition in criteria
$criteria->condition='subject_id=1 or subject_id=3';
when i put the condition in criteria its filter don't work any solution for having condition in search function and its filter work too
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('slide_num',$this->slide_num,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('size',$this->size,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('subject_id',$this->subject_id);
$criteria->condition='subject_id=1 or subject_id=3';
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
/*return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));*/
}
Instead of $criteria->condition='subject_id=1 or subject_id=3';
try
$criteria->addCondition("subject_id = 1");
$criteria->addCondition("subject_id = 2", "OR");
OR JUST
$criteria->addInCondition("subject_id", array("1","2"));

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

restrict selected columns in cakephp query

I want to reduce the number of fields returned by cakephp's find('all') but don't know if this is possible.
$this->Group->find('all', $params);
where $params
$params = array(
'conditions' => array(
'Group.featured' => 1,
),
'contain' => array(
'User',
'Class' => array(
'conditions' => array(
'Class.exp IS NOT NULL',
'Class.tb <20',
)
)
)
));
The problem is that my Class table has many columns that i don't need and that take a long time to load, so i would line to only select 5 fields.
Can this be done in Cakephp or am i better off writing a regular query?
something like
$params = array(
'conditions' => array(
'Group.featured' => 1,
),
'contain' => array(
'User',
'Class.a',
'Class.b',
'Class.exp',
'Class.tb',
'Class' => array(
'conditions' => array(
'Class.exp IS NOT NULL',
'Class.tb <20',
)
)
)
));
Thank you
That's what the fields parameter is for.
$params = array(
...
'contain' => array(
'Class' => array(
'conditions' => array(
'Class.exp IS NOT NULL',
'Class.tb <20',
),
'fields' => array('a', 'b')
)
)
);