How to set current page with Yii's CActiveDataProvider? - yii

I've created this CActiveDataProvider:
$dataProvider=new CActiveDataProvider('Post',array(
'criteria'=>array(
'condition'=>$condition,
'params'=>$params,
),
'pagination'=>array(
'pageSize'=>2,
),
));
but ... How can I define current page and load records 3,4 instead of 1,2?
How can I get a precise page?

Set the zero-based currentPage attribute of the pagination
...
'pagination'=>array(
'pageSize'=>2,
'currentPage'=>1,
),
...

Related

How to get names in alphabetic order in Yii

In my Yii web application, having student table for saving student details. I want to student name in alphabetical order when retrieve data from student table.
public function defaultScope() {
return array("order" => "student_firstname");
}
I tried this function, but not working properly.
Please help me.
Thanks in advance.
you can use the defaultOrder property of CSort. For example...
$dataProvider=new CActiveDataProvider('Example', array(
'sort'=>array(
'defaultOrder'=>array(
'student_firstname'=>false
)
)
));
In model's search method for data provider try to set default order, like this:
$dataProvider=new CActiveDataProvider($this, array(
'sort'=>array(
'defaultOrder'=> 'student_firstname ASC'
)
));
In model's search method for data provider try to set default order, like this:
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>Yii::app()->params['defaultPageSize'],
),
'sort'=>array(
'defaultOrder'=>array(
'student_firstname'=>CSort::SORT_ASC
),
),
));

CListView filtering by value

is there a parameter that I can use with ClistView to get the commments of a given post_id
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comment',
'template'=>"{items}\n{pager}",
)); ?>
I'd like to add post_id = value in order to only list the comment with the given post_id
I put a search function in my comment model
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('post_id',$this->post_id,true);
return new CActiveDataProvider('Comment', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'status, create_time DESC',
),
));
}
Thank in advance for your help. I cannot find in the documentation.

cgridveiw to display fetched data via customized query

I am a newbei in yii, when a person clicks on a category display him all products under that particular category in a gridview
view
productcategory
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'admin-grid',
'dataProvider'=>$model->SelectedCategoryProducts,
'filter'=>$model,
'columns'=>array(
'Name',
'Model',
'Brand',
'Price',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
controller
product
public function actionProductcategory($id)
{
$model= Product::model()->SelectedCategoryProducts($id);
var_dump($model);
$this->render('productcategory',array(
'model'=>$model,'id'=>$id,
));
}
model
product
public function SelectedCategoryProducts($id)
{
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
var_dump($dataProvider);
return $dataProvider;
}
CException
Property "CActiveDataProvider.sellerSelectedCategoryProducts" is not defined.
PLEASE HELP! I am losing my mind on this ... not able to display in gridview.
Pass $id to your view file.
$this->render('productcategory',array('model'=>$model,'id'=>$id));
Then pass id to model function in ccgridview function.
'dataProvider'=>$model->SelectedCategoryProducts($id),
Hope this might help
Controller file
public function actionProductcategory($id)
{
$model=new Product;
$this->render('productcategory',array('model'=>$model,
'id'=>$id));
}
In View file
'dataProvider'=>$model->SelectedCategoryProducts($id),
UPDATE 1
'columns'=>array(
'name',
'model',
'brand',
'price',
change them to lowercase which are your original column names
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
this can retrieve needed data.... first check that data is perfect ... is it right after that take second step....

How to set defalut value in CGridView

How can I set default value if $data->pic=="" in my dataprovider values. Set data pic as na.jpg
widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'type'=>'raw',
'name'=>'pic',
'value'=>'CHtml::image("http://localhost/studentpics/".$data->pic)',
),
));
widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'your Image URL',
'type'=>'image',
'htmlOptions'=>array('width'=>'30px','height'=>'30px'),
),
));
value is evaluated as a php expression so it is ok to use a condition in it
'value'=>'CHtml::image(($data->pic)?"http://localhost/studentpics/".$data->pic:"default_image_url")',
P.S it's a bad idea to use absolute url's everywhere.
I think you can override afterFind method of CActiveRecord
protected function afterFind()
{
if($this->pic ===null)
{
$this->pic = na.jpg;
}
parent::afterFind();
}

Pass additional parameters to pagination url

Im using CArrayDataProvider for pagination. Now only page numbers are passed in url. I want to pass some more additional attributes. How do I do that? Here is my dataprovider.
$dataProvider = new CArrayDataProvider($arrayData, array(
'keyField'=>'entity_id',
'sort'=>array(
'attributes'=>array('entity_id'),
'defaultOrder'=>array('entity_id' => false),
),
'pagination'=>array(
'pageSize'=>20,
),
));
CPagination takes params. Try adding 'params' => array('foo' => 'bar') in pagination.
The question was how to pass "additional" parameters.
The code of the accepted answer will override your existing $_GET parameters:
'params' => array('foo' => 'bar')
To add "additional" parameters, first add them to existing $_GET parameters:
$pagerparams = $_GET;
$pagerparams['foo'] = 'bar';
$dataProvider = new CActiveDataProvider($this, array(
'pagination'=>array(
'params' => $pagerparams,
),
...
));