`

Cakephp中自定义查询的分页问题,解决方法

阅读更多
转载  Cakephp中自定义查询的分页问题,解决方法 收藏

Cakephp的默认分页功能是基于内置的表关联模型的,所以,如果我们有更多复杂的要求,比如:

    联合两张表的查询分页

这样Cakephp的默认分页就达不到我们的要求,解决的方法是:

使用自定义的查询分页

使用自定义查询分页需要在模型中重载两个方法,paginate()和paginateCount()

1. paginate()

提供分页的数据支持,原型如下:
1. function paginate( $conditions , $fields , $order , $limit , $page = 1, $recursive = null);

参数由Cakephp自定传递,使用方法和find方法是一样的,参数通过在Controller中定义paginate变量传递,例如:
1. $limit = 5;
2. $this ->paginate = array (
3.      'SavedNote' => compact( 'limit' )
4. );

注意:

如果重载paginate()的函数使用参数,请自己格式化参数为自己定义的格式,Cakephp不会自动格式化参数。

2. paginateCount()

提供分页的数据数量统计支持,原型如下:
1. function paginateCount( $conditions = null, $recursive = 0);

带来的问题

通过上述的方法确实能够给我们的自定义查询分页,但是问题是,这样做之后我们无法再使用原有的模型的分页。

解决方法:

使用额外的模型完成我们的自定义查询任务

我们新建一个模型,/models/saved_note.php ,代码如下:
view source
< id="highlighter_464972_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" id="highlighter_464972_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_464972" menu="false" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" width="16" height="16">
print ?
01. <?php
02. class SavedNote extends AppModel {
03.      var $name = 'SavedNote' ;
04. 
05.      //这个模型不使用任何表,它仅仅为我们提供自定义分页支持
06.      var $useTable = false;
07.      var $user_id = 0;
08. 
09.      /*
10.       * Paginate Function
11.      */
12.      function paginate( $conditions , $fields , $order , $limit , $page = 1, $recursive = null) {
13.          $offset = ((int) $page - 1) * (int) $limit ;
14.          $conditions = "
15.          //这里省略的SQL会在附录中列出
16.          ";
17.          return $this ->query( $conditions );
18.      }
19. 
20.      /*
21.       * Paginate Count Function
22.      */
23.      function paginateCount( $conditions = null, $recursive = 0) {
24.          $conditions = "
25.          //这里省略的SQL会在附录中列出
26.          ";
27.          $result = $this ->query( $conditions );
28.          return (int) $result [0][0][ 'NUMS' ];
29.      }
30. }
31. ?>

我们可以在控制器中使用这个自定义查询了:
view source
< id="highlighter_859578_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" id="highlighter_859578_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_859578" menu="false" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" width="16" height="16">
print ?
01. class NotesController extends AppController{
02.      var $name = 'Notes' ;
03.      var $layout = 'notes' ;
04. 
05.      //这里引入我们需要的模型
06.      var $uses = array ( 'Note' , 'SavedNote' , 'TrashNote' );
07. 
08.      function saved(){
09.          $user_id = $this ->Auth->User( 'id' );
10.          $limit = 5;
11. 
12.          //这里为我们的自定义查询,传递一些参数
13.          $this ->paginate = array (
14.              'SavedNote' => compact( 'limit' )
15.          );
16.          $this ->SavedNote->user_id = $user_id ;
17. 
18.          //控制器的paginate方法,会自定寻找模型的相应方法,并调用
19.          $notes = $this ->paginate( 'SavedNote' );
20.          $this ->set(compact( 'notes' ));
21.      }
22. 
23. }

最后,希望大家看到的是,自定义模型中的paginate参数的使用是需要自己手动解析和分配的,如果要使用Cakephp的默认查询样式,还需要自己解析一下。

附录:

1. 模型中paginate方法用到的SQL
view source
< id="highlighter_998076_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" id="highlighter_998076_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_998076" menu="false" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" width="16" height="16">
print ?
01. SELECT
02.      DISTINCT (SavedNote.note_id),
03.      SavedNote.*,
04.      Note.subject,
05.      Note.body,
06.      Note.created,
07.      SentUser. name ,
08.      ReceivedUser. name
09. FROM (
10.      SELECT
11.          ReceivedNote.note_id AS note_id,
12.          ReceivedNote.receive_user_id AS receive_user_id,
13.          ReceivedNote.send_user_id AS send_user_id,
14.          ReceivedNote.is_deleted AS is_deleted,
15.          ReceivedNote.is_saved AS is_saved,
16.          ReceivedNote.read_date AS read_date,
17.          'received' as src
18.      FROM
19.          received_notes AS ReceivedNote
20.      WHERE
21.          receive_user_id = {$this->user_id}
22.      UNION ALL
23.      SELECT
24.          SentNote.note_id AS note_id,
25.          SentNote.receive_user_id AS receive_user_id,
26.          SentNote.send_user_id AS send_user_id,
27.          SentNote.is_deleted AS is_deleted,
28.          SentNote.is_saved AS is_deleted,
29.          SentNote.read_date AS read_date,
30.          'sent' as src
31.      FROM
32.          sent_notes AS SentNote
33.      WHERE
34.          send_user_id = {$this->user_id}
35. ) as SavedNote
36. LEFT JOIN notes AS Note ON Note.id = SavedNote.note_id
37. LEFT JOIN users AS SentUser ON SentUser.id = SavedNote.send_user_id
38. LEFT JOIN users AS ReceivedUser ON ReceivedUser.id = SavedNote.receive_user_id
39. WHERE
40. SavedNote.is_saved = 1
41. ORDER BY
42. Note.created DESC
43. LIMIT {$offset},{$limit}

2. 模型中paginateCount方法用到的SQL
view source
< id="highlighter_96965_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" id="highlighter_96965_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_96965" menu="false" src="http://www.thinkly.cn/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" width="16" height="16">
print ?
01. SELECT
02.      COUNT ( DISTINCT (SavedNote.note_id)) AS NUMS
03. FROM (
04.      SELECT
05.          ReceivedNote.note_id AS note_id,
06.          ReceivedNote.receive_user_id AS receive_user_id,
07.          ReceivedNote.send_user_id AS send_user_id,
08.          ReceivedNote.is_saved AS is_saved,
09.          'received' as src
10.      FROM
11.          received_notes AS ReceivedNote
12.      WHERE
13.          receive_user_id = {$this->user_id}
14.      UNION ALL
15.      SELECT
16.          SentNote.note_id AS note_id,
17.          SentNote.receive_user_id AS receive_user_id,
18.          SentNote.send_user_id AS send_user_id,
19.          SentNote.is_saved AS is_saved,
20.          'received' as src
21.      FROM
22.          sent_notes AS SentNote
23.      WHERE
24.          send_user_id = {$this->user_id}
25. ) as SavedNote
26. WHERE
27. SavedNote.is_saved = 1
分享到:
评论

相关推荐

    CakePHP中PageHelper的分页应用

    总的来说,CakePHP的PageHelper是实现分页功能的强大工具,它简化了分页的实现,提高了开发效率,并且允许开发者通过配置和自定义方法来满足各种复杂的分页需求。通过学习和熟练掌握PageHelper的使用,你可以创建更...

    cakephp分页.docx

    cakephp分页,cakephp框架的分页的方法的文档,是cakephp框架的内容。

    cakephp-containable行为behaviour

    2:cakephp中虽然对模型的操作方法中并没有提供像上面提到的连贯方法 但是cake框架提供了一个containable行为类来更好的完善模型关联中遇到的上述问题 "&gt;1:thinkphp中通过框架提供的连贯操作Model &gt;join ‘other ...

    cakephp中文手册API

    在CakePHP中,模型通常与数据库表关联,用于执行CRUD(创建、读取、更新、删除)操作。ORM(对象关系映射)是模型层的核心,使得开发者可以用面向对象的方式来操作数据库。 4. **视图(View)**: 视图负责展示...

    cakephp2.X多表联合查询join及使用分页查询的方法

    在CakePHP中,这通常通过在`find`方法或`paginate`方法中指定`joins`参数来实现。例如,这里的`paginate`配置中,有三个JOIN操作: 1. `UserInfo`表通过`USERID`字段与`LoginHistory`表关联,定义为LEFT JOIN,意味...

    CakePHP 1.2 API 中文文档

    CakePHP 的路由系统允许开发者自定义URL结构,使得URL更符合人类阅读习惯。通过配置路由规则,可以将任意URL映射到特定的控制器和动作。 **表单验证(Validation)** 在模型层,CakePHP 提供了强大的表单验证机制。...

    cakephp 中文手册

    2. **查询构造器**:通过ORM(对象关系映射),开发者可以使用链式方法构建复杂的SQL查询,无需直接编写SQL语句。 ### 四、验证和错误处理 1. **数据验证**:在模型层,可以设置数据验证规则,确保输入数据的完整...

    cakephp中文手册

    5. **社区支持**:由于有庞大的社区支持,开发者可以找到许多插件和教程来增强功能或解决遇到的问题。 6. **错误和日志管理**:CakePHP 提供了强大的错误报告和日志记录功能,帮助开发者追踪和调试问题。 7. **...

    cakephp中acl详解.rar

    在CakePHP中,ACL由三个主要部分组成:Aro(Access Request Object),Aco(Access Control Object)和Ace(Access Control Entry)。Aro代表请求访问的主体,如用户或角色;Aco代表可被访问的客体,如控制器、动作...

    cakephp 中文文档

    全面的cakephp中文文档

    cakephp中acl详解

    在CakePHP中,Access Control List(ACL)是一个强大的工具,用于管理应用程序中的权限和访问控制。让我们深入探讨一下CakePHP中的ACL系统。 ### ACL基础概念 **1. ACO (Access Control Object)**:ACO代表要控制...

    cakePHP 中文手册

    CakePHP有多个特点,这些特点让CakePHP成为了快速开发框架中的佼佼者之一。 1.活跃友好的社区 2.灵活的许可协议(Licensing) 3.兼容PHP4和PHP5 4.数据库交互和简单查询的集成 5.应用程序Scaffolding 6....

    cakephp中文手册.pdf

    你可以通过升级 CakePHP 版本来更新这些文件,而不用担心覆盖自定义的应用程序代码。 3. **vendors**:这个目录用于存放第三方库或组件。通过`vendor()`方法可以方便地调用这些外部库。 完整的目录结构还包含其他...

    CakePHP中文手册

    CakePHP中文手册

    PHP的框架之CakePHP-CakePHP教程

    CakePHP的分页组件源码;CakePHP的中文及英文教程,CHM格式;CakePHP的中文打印版教程,WORD格式,下载后可直接打印,方便的;CakePHP的HTML格式的教程;CakePHP的使用技巧介绍;CakePHP开发的网站源码参考;CakePHP...

    cakephp-1.2 manual

    10. **调试与性能优化**:学习如何使用 CakePHP 的调试模式进行问题排查,以及如何进行性能优化,如使用缓存和优化查询。 11. **插件(Plugins)**:CakePHP 插件允许你重用和扩展框架的功能,手册会解释如何安装、...

    PHP的框架之CakePHP-CakePHP教程终极教程

    CakePHP的分页组件源码;CakePHP的中文及英文教程,CHM格式;CakePHP的中文打印版教程,WORD格式,下载后可直接打印,方便的;CakePHP的HTML格式的教程;CakePHP的使用技巧介绍;CakePHP开发的网站源码参考;CakePHP...

    mvc,mvc手册cakephp中文手册

    在CakePHP中,MVC的实现十分直观。例如: - **模型(Model)**:开发者可以创建对应的数据库表类,继承自CakePHP的`AppModel`,用于操作数据库。模型类提供了数据查询和修改的方法,如`find()`和`save()`。 - **...

Global site tag (gtag.js) - Google Analytics