Reputation: 2133
I am trying to force a join on paginate function of cakephp. A user has messages which means it will be message belongs to user. I have to show them in a list so i need to use paginate here. Problem is it doesnt shows me the record of the model which i intend to bind My code is:
$userId = $this->Session->read('SESSION_ADMIN.id');
$this->helpers['Paginator'] = array('ajax' => 'Ajax');
$this->Message->bindModel(
array(
'belongsTo'=>array(
'Npo'=>array(
'className' => 'Npo',
'foreignKey' => 'reciever_id',
'fields' => 'Npo.username'
)
)
)
);
$this->paginate = array('conditions'=>array('Message.sender_id'=>$userId,'Message.sender'=>'Admin'),
'order' => array('Message.modified DESC'),
'limit' =>'1'
);
$sentMsg = $this->paginate('Message');
//$sentMsg = $this->Message->find('all');
pr($sentMsg);die();
when i uncomment the FIND statement it shows me record but in case of paginate it doesnt. Also it doesnt shows me the join in the paginate query but it does in counting record. Any one have an idea.I dont want to use paginate Join here.Is there a way to enforce a belongs to here?
Regards Himanshu Sharma
Upvotes: 0
Views: 3789
Reputation: 2978
Have you tried:
$this->Message->bindModel(
array(
'belongsTo'=>array(
'Npo'=>array(
'className' => 'Npo',
'foreignKey' => 'reciever_id',
'fields' => 'Npo.username'
)
)
), false // Note the false here!
);
The paginator actually executes two queries: one to count the total number of records, and one to actually fetch the desired records. By default, associations created on the fly using bindModel()
are reset after each query. It depends on the Cake version which query comes first, but I believe that in your case it is the count query; leaving the actual results query without the association. Setting false
on on the second argument of bindModel() prevents the association from being reset after the first query.
Upvotes: 8