简体   繁体   中英

Forcing a belongs to relation in cakephp paginate

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

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.

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