Paginate from within a model in CakePHP

前端 未结 4 787
情深已故
情深已故 2020-12-01 11:25

I have a function in my Event model called getEvents - you can pass limit, start and end dates, fields,

相关标签:
4条回答
  • 2020-12-01 12:02

    $paginate array are similar to the parameters of the Model->find('all') method, that is: conditions, fields, order, limit, page, contain, joins, and recursive.

    So you can define your conditions like this :

    var $paginate = array(
     'Event' => array (...)
     );
    

    Or you can also set conditions and other keys in the $paginate array inside your action.

     $this->paginate = array(
     'conditions' => array(' ... '),
     'limit' => 10
     );
     $data = $this->paginate('Event');
    
    • http://book.cakephp.org/2.0/en/controllers.html
    • http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

    R u using $name = 'Event' in your controller ?

    If we wont mention model name in $this->paginate() , it will use model as mentioned in $name otherwise look in var $uses array and in that will get Model name (first one )

    for e.g var $uses = array('Model1','Model2'); // $name != mentioned

    n you want pagination with respect to Model2 then you have to specify ModelName in paginate array like $this->paginate('Model2') otherwise Model1 will be considered in pagination.

    0 讨论(0)
  • 2020-12-01 12:11

    The way I figured out how I can keep my complex find in my model without having to rewrite it a second time in the controller is by passing a $paginate boolean variable.

    If $paginate is true, it returns just the options created, which can then be used in the controller's pagination. If it's false (meaning we don't want to paginate), it returns the actual event results. So far this seems to be working.

    In my getEvents() function (this method is in the Events model)

        if($paginate) {
            return $qOpts; // Just return the options for the paginate in the controller to use
        } else {
            $data = $this->find('all', $qOpts); // Return the actual events
            return $data;
        }
    

    Then, in my Events/Index (events controller, index action - where I know I want pagination):

    $this->Event->recursive = -1; // (or set recursive = -1 in the appModel)
    $opts['paginate'] = true;
    
    $paginateOptions = $this->Event->getEvents($opts);
    
    $this->paginate = $paginateOptions; // Set paginate options to just-returned options
    $data = $this->paginate('Event'); // Get paginate results
    $this->set('data', $data); // Set variable to hold paginated results in view
    
    0 讨论(0)
  • 2020-12-01 12:12

    you can use this one which is working fine for me.

    $condition="your where condition"; $this->paginate = array( 'fields' => array('AsinsBookhistory.id', 'AsinsBookhistory.reffer_id', 'AsinsBookhistory.ISBN','AsinsBookhistory.image','AsinsBookhistory.title','AsinsBookhistory.last_updatedtime'), 'conditions' => $condition, 'group' => array('AsinsBookhistory.ISBN'), 'order' => array('AsinsBookhistory.last_updatedtime' => 'desc') ); $this->set('lastvisitedbooks', $this->paginate('AsinsBookhistory'));

    0 讨论(0)
  • 2020-12-01 12:15

    The paginate() model method does not accept the same parameters as a find(). Specifically, find() wants an array of options, but paginate() wants every option passed individually. See Custom Query Pagination in the CakePHP book.

    So, instead of:

    $data = $this->paginate($qOptions);
    

    You want something like:

    $data = $this->paginate($qOptions['conditions'], $qOptions['fields'], ...);
    

    EDIT

    Custom model pagination isn't a function that you call. It's a function that you need to implement and will be called by the CakePHP framework. In the example in your question you are trying to manually call $this->paginate(...) from somewhere in your model. That doesn't work. Instead, do this.

    In your model, implement the paginate and paginateCount methods.

    function paginate($conditions, $fields, ...)
    {
        // return some data here based on the parameters passed
    }
    
    function paginateCount($conditions, ...)
    {
        // return some rowcount here based off the passed parameters
    }
    

    Then, in your controller you can use the standard pagination functions.

    function index()
    {
        $this->paginate = array('MyModel' => array(
            'conditions' => array(...),
            'fields' => array(...),
        ));
    
        $this->set('myobjects', $this->paginate('MyModel'));
    }
    

    Now, the Controller::paginate() function will grab the conditions and other data from the Controller::paginate parameter and, instead of passing it to your Model::find it will pass it to your custom Model::paginate() and Model::paginateCount() functions. So, the data that is returned is based on whatever you do in those two methods and not based on a standard find(). }

    0 讨论(0)
提交回复
热议问题