doubei8168 2011-10-18 13:21
浏览 28
已采纳

在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

  • 写回答

1条回答 默认 最新

  • dtip91401 2011-10-18 13:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)