免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2286 | 回复: 0
打印 上一主题 下一主题

Cakephp如何在paginate使用unbind [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-10 23:03 |只看该作者 |倒序浏览
作者:bkkkd
原文地址:Cakephp如何在paginate使用unbind
cakephp的controller中的paginate是一个得到分页数据的函数.配合helper里的Paginator,可以很轻松的做出分页列表,排序的列表页面.
但由我开始学习用cakephp时,我就有一个问题一直困扰着我.
Model如何解除关联(unbind)?
正常的情况下,只要在find之前解除(unbind)我不需的model.就可以不去搜索这些modeld关联的数据表.而且在find完以后会自动返把之前我解除的model再次关联起来.以下是常用的使用方法

  1. //user model
  2. class User extends AppModel {

  3.   var $name = 'User';
  4.   var $belongsTo = array(
  5.             'Profile' = array('className'=>'Profile','foreignKey'=>'user_id')
  6.           )
  7. }
复制代码
运行以下代码

  1. $this->User->unbind(array('belongsTo'=>array('Profile')));
  2. $rs=$this->User->find();
复制代码
$rs会是

  1. array(
  2.        'User'=>array(),
  3. )
复制代码
如果在find之前没有运行unbind,$rs将会是

  1. array(
  2.        'User'=>array(),
  3.        'Profile'=>array()
  4. )
复制代码
但如果运行paginate就得不到同样的结果

  1. $this->User->unbind(array('belongsTo'=>array('Profile')));
  2. $rs=$this->paginate('User');
复制代码

$rs的结果还是

  1. array(
  2.        'User'=>array(),
  3.        'Profile'=>array()
  4. )
复制代码
为什么在paginate不能解除关联(unbind)?
原因是在find里在得到数据后,find会用model->resetAssociations();把所有关联(Association)还原.而paginate里使用了两次find.一次是得到总数,另一次得到分页显示的数据.所以返回的结果还是有Profile的内容.
解决方法:给unbind的第二个参数里赋上非ture的值.如果unbind的第二个参数是true,cakephp会把需要解除关联的数据库保存到model->__backAssociation里,当运行model->resetAssociations();会从model->__backAssociation把相关的关联的数据还原.所以以下代码就可以解决
code]
$this->User->unbind(array('belongsTo'=>array('Profile')),false);
$rs=$this->paginate('User');
[/code]
另外,如果在运行paginate()后,还需要使用model里的关联数据来find 数据.可以在app_model.php文件里增加以下代码

  1.   /**
  2.      * function description:turn off the Association,and return the the Association,.
  3.    *                      the function working for Controller->paginate() and Model->bind().
  4.    *                      the function will help you that get data form Controller->paginate() before unbind some
  5.    *                      Association for and rebind the remove of Association after get data.
  6.    *                      if you don't neet to rebind Association,you can only use
  7.    *                      <code>
  8.    *                      $this->Models->unbind($params,false);
  9.    *                      </code>
  10.      * @Date:2008-10-10
  11.    * <code>
  12.    * $backAssociation = $this->ModelName->unbindAndPushModels(array('belongsTo'=>array('User')));
  13.    * $result=$this->paginate('ModelName');
  14.    * $this->ModelName->bind($backAssociation);//this action is to restore the model of assocication data.
  15.    * </code
  16.      * @param    (类型)参数名  :描述
  17.   **/
  18.     Function unbindAndPushModels($params)
  19.     {
  20.     $backAssociation=array();
  21.       foreach ($params as $assoc => $models)
  22.     {
  23.       foreach ($models as $model)
  24.       {
  25.         If(isset($this->{$assoc}[$model]))
  26.         {
  27.           $backAssociation[$assoc][$model] = ;
  28.           unset ($this->{$assoc}[$model]);
  29.         }  
  30.       }
  31.     }
  32.     Return $backAssociation;
  33.     }
复制代码

Cakephp

[ 本帖最后由 bkkkd 于 2008-10-11 10:00 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP