I have a function in my Event
model called getEvents
- you can pass limit, start
and end
dates, fields
, event
types
, and event subtypes
.
I read that paginate
can accept all the parameters I'm using like joins
, conditions
, limit
...etc just like a normal find
can.
It returns data just fine when I don't try to paginate. But - I'd like to be able to pass it a paginate
variable to tell it instead of doing this:
$this->recursive = -1;
$data = $this->find('all', $qOptions);
to do this:
$this->recursive = -1;
$data = $this->paginate($qOptions);
When I try that, though, it gives me lots of errors. I can specify the errors later if needed - for now, I guess I'm looking for - is this something that can be done? If so, how?
Is there another better way to do something like this? I spent enough time making this function do just what I want, and allowing all the options passed...etc - it just seems like a waste if I can't also use it for pagination. But - if it's not ideal, I'm ok hearing that too. Thanks in advance.
EDIT:
I'm reading other things online that say you shouldn't use paginate in your model, because it draws from URL variables, which defeats the MVC structure purpose. This makes sense, but does that mean I have to write the same joins/queries in both model and controller? And in every action that it's needed?
The
paginate()
model method does not accept the same parameters as afind()
. Specifically,find()
wants an array of options, butpaginate()
wants every option passed individually. See Custom Query Pagination in the CakePHP book.So, instead of:
You want something like:
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
andpaginateCount
methods.Then, in your controller you can use the standard pagination functions.
Now, the
Controller::paginate()
function will grab the conditions and other data from theController::paginate
parameter and, instead of passing it to yourModel::find
it will pass it to your customModel::paginate()
andModel::paginateCount()
functions. So, the data that is returned is based on whatever you do in those two methods and not based on a standardfind()
. }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)Then, in my Events/Index (events controller, index action - where I know I want pagination):
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'));
$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 :
Or you can also set conditions and other keys in the $paginate array inside your action.
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 invar $uses
array and in that will get Model name (first one )for e.g
var $uses = array('Model1','Model2');
// $name != mentionedn 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.