Hello all, i am new to Yii and this forum. I am starting a new project and after some researchs i have decided to learning this framework and use it.
Now i am stuck with the pagination. I have a form, sending by POST only 2 variables to an action. This action generates the pagination, and the first page is shown well, but if i try to navigate through the CLinkPager there is no data shown, even the CLinkpager itself has disappeared.
I have 3 days lost yet with this problem. I have read almost every pagination related post without finding a solution, and i have changed my code almost 100 times. First i tried to paginate from a model function directly (that returned a CActiveDataProvider) but i was unable to even show the first page. I have seen all the pagination examples triggered from the controller, i mean always the querys are executed from the actions in the controller (in order to paginate the data).
There is no way to paginate a CActiveDataProvider object directly, for example returned from a model function?
Now the code is this, works for only first page:
Controller:
public function actionIndexSearch() {
//here some trivial validations and a debug echo
if ($_POST['city'] != null && isset($_POST['type'])) {
//I tried also defining constants for ensure the data is not lost by pagination (maybe recalling this action again without POST data...)
//define(_CITY, $_POST['city']);
//define(_TYPE, $_POST['type']);
$cityId = $_POST['city'];
$type = $_POST['type'];
echo "dentro";
}
//Another echo for watching the behavior.
echo "fuera";
//Creating the data to paginate and its pagination:
$c = new CDbCriteria();
$location = Location::model()->tableName();
$c->compare('t.event_type_id', $type, false);
$c->join = 'inner join ' . $location . ' l on l.city_id = "' . $cityId . '"';
$itemcount = LiveEvent::model()->count($c);
$pages = new CPagination($itemcount);
$pages->setPageSize(2);
$pages->applyLimit($c);
Yii::app()->setTheme('urbandow');
$this->layout = 'main';
$this->render('eventsResults', array(
'data' => LiveEvent::model()->findAll($c),
'pages' => $pages,
//'cityId' => $cityId,
//'type' => $type,
));
}
In my view, for example:
<?php
$this->widget('CLinkPager', array(
'pages' => $pages,
));
?>
<?php
foreach ($data as $data1){
echo $data1->title;
}
?>
Ok, and in my model i had this (now it is not used, but i think if we want put all the logic in the models there should be a way to paginate it…):
public static function listByCity($cityId, $type) {
$c = new CDbCriteria();
$location = Location::model()->tableName();
$c->compare('t.event_type_id', $type, false);
$c->join = 'inner join ' . $location . ' l on l.city_id = "' . $cityId .'"';
return new CActiveDataProvider($this, array(
'criteria' => $c,
'pagination' => 2,
));
}
Thank you very much!