简体   繁体   中英

Yii: Pagination not working

I am new in yii framework. creating a job site in yii.My controller is sitecontroller.php,view file is list_jobseeker.php.I got the error Property "CDbCriteria.0" is not defined.

Controller

<?php
  public function actionlist_jobseeker()
    {
      $items = Yii::app()->db->createCommand()
     ->select('u.name,u.id uid, u.email, j.title jTitle,  j.experience,  s.contact_no, s.gender')
     ->from('user u')
     ->join('job_seeker_profile s','u.id = s.user_id')
      ->join('job_profile j','u.id = j.user_id')
      ->join('location l','l.id = s.location_id')
      ->join('category c','c.id = j.category_id')
      ->where('u.role=:role', array(':role'=>'user'))
     ->order('u.id')
     ->queryAll();
      $count=Job::model()->count($items);
    $pages=new CPagination($count);
    $pages->pageSize=2;
    $pages->applyLimit($items);
    $number_rec=count($items);
     $this->render('list_jobseeker',array('items' =>$items,'pages' => $pages));
    }

?>

Pagination display code in view file,list_jobseeker is

  <p ><?php $this->widget('CLinkPager', array(
   'pages' => $pages,
   )) ?></p> 

Anybody give any suggestion plz?

In your code you execute the query before applying the pagination, so it doesn't make sense.

The problem come from your applyLimit

$pages=new CPagination($count);
$pages->pageSize=2;
$pages->applyLimit($items);

You give it an array of objects already fetched and according to the documentation it takes a CDbCriteria object.

You should perform something like

//Set the criteria for the request
$criteria = new CDbCriteria;
$criteria->select = "..."; //your columns
$criteria->condition = "..."; //your condition
$criteria->order = "..."; //your order
$criteria->with = "..."; //the join you wish to make

//Set the pagination for the request
$count = User::model()->count($criteria);
$pages=new CPagination($count);
$pages->pageSize=2;
$pages->applyLimit($criteria);

//Find the matching items
$items = User::model()->findAll($criteria);

Documentation of the CDbCriteria

Edit : if you really want to use createCommand check this forum post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM