在 cakephp 分页中强制属于关系
我试图强制加入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过:
分页器实际上执行两个查询:一个用于计算记录总数,另一个用于实际获取所需的记录。默认情况下,使用
bindModel()
即时创建的关联会在每次查询后重置。这取决于 Cake 版本,哪个查询先出现,但我相信在你的情况下它是计数查询;留下没有关联的实际结果查询。在 bindModel() 的第二个参数上设置false
可防止在第一个查询后重置关联。Have you tried:
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. Settingfalse
on on the second argument of bindModel() prevents the association from being reset after the first query.