在 cakephp 分页中强制属于关系

发布于 2024-12-10 07:20:31 字数 1129 浏览 0 评论 0原文

我试图强制加入 cakephp 的分页功能。 用户有消息,这意味着该消息属于该用户。 我必须在列表中显示它们,所以我需要在这里使用分页。 问题是它没有显示我打算绑定的模型的记录 我的代码是:

$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();

当我取消注释 FIND 语句时,它会显示记录,但在分页的情况下它不会。 此外,它没有显示分页查询中的连接,但它显示了计数记录。 任何人都有一个想法。我不想在这里使用分页加入。有没有一种方法可以强制属于这里?

问候 希曼舒·夏尔马

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2024-12-17 07:20:31

您是否尝试过:

$this->Message->bindModel(
           array(
             'belongsTo'=>array(
                 'Npo'=>array(
                   'className'  =>  'Npo',
                 'foreignKey' => 'reciever_id',
                 'fields'     => 'Npo.username'
               )          
           )
        ), false // Note the false here!
    );

分页器实际上执行两个查询:一个用于计算记录总数,另一个用于实际获取所需的记录。默认情况下,使用 bindModel() 即时创建的关联会在每次查询后重置。这取决于 Cake 版本,哪个查询先出现,但我相信在你的情况下它是计数查询;留下没有关联的实际结果查询。在 bindModel() 的第二个参数上设置 false 可防止在第一个查询后重置关联。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文