CakePHP自定义查询Paginator

时间:2015-06-30 06:06:06

标签: cakephp pagination

目前我有2页要求分页。 我也做了一些研究,如设置或模型做自定义查询。然而,Paginator无法工作。

现在我正在尝试将自定义查询分页器添加到索引页面。 (我需要使用分组) 我还尝试向查看页面添加自定义查询分页器。 (我需要基于id)

控制器:

public function index() {    
    $setting =  $this->Paginator->settings = $this->Report->query('
        SELECT SUM( seats_occupied ) as totalOccupied, event_date, r.event_date_id
        FROM reports r, event_dates ed WHERE r.event_date_id = ed.event_date_id
        GROUP BY event_date
    ');

    debug($this->Paginator->paginate());
    $this->set('showDate',$setting);
}

public function view($eventDateId = null){
    if (!$eventDateId) { 
        throw new NotFoundException(__('Invalid post'));
    }
    // We only want single post so we use FindById.
    $post = $this->Report->findAllByEventDateId($eventDateId);
    // debug($this->Paginator->paginate($post));
    if (!$post) {
        //if user requet fypcakephp/view without id will throw the not found except
        throw new NotFoundException(__('Invalid post'));
    }

    $setting = $this->Paginator->settings = $this->Report->query("
        SELECT *
        FROM reports where event_date_id='$eventDateId' order by period asc
    ");

    //array( 'conditions' => array( 'event_date_id' => $post));

    debug($setting);
    $this->set('viewData',  $setting); //Then send this data
}

我也尝试过模型自定义查询然而如果我这样做,由于我使用“分组依据”,我的视图无法获取我想要的数据。

//To custom the paginate display the thing you want. (This one return)
//public function paginate($conditions, $fields, $order, $limit, $page = 1,
//    $recursive = null, $extra = array())
//{    
//    $recursive = 0;
//
//    // Mandatory to have
//    $this->useTable = false;
//    $sql = '';
//
//   
//         $sql .= " select * from reports;";
//
//    
//
//            $sql .= " SELECT SUM( seats_occupied ) as totalOccupied, event_date,r.event_date_id
//            FROM reports r, event_dates ed WHERE r.event_date_id = ed.event_date_id
//            GROUP BY event_date; ";
//
//
//
//    // Adding LIMIT Clause
////    $sql .= (($page - 1) * $limit) . ', ' . $limit;
//
//    $results = $this->query($sql);
//
//    return $results;
//}

//public function paginateCount($conditions = null, $recursive = 0, $extra = array())
//{
//    $sql = '';
//
//    $sql .= "SELECT SUM( seats_occupied ) as totalOccupied, event_date ,r.event_date_id 
//            FROM reports r, event_dates ed WHERE r.event_date_id = ed.event_date_id
//            GROUP BY event_date";
//
//    $this->recursive = $recursive;
//
//    $results = $this->query($sql);
//
//    return count($results);
//}

0 个答案:

没有答案