- 浏览: 348790 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (238)
- j2ee (22)
- mysql (14)
- hibernate (2)
- struts (3)
- spring (7)
- php (28)
- cakephp (12)
- pattern (0)
- 数据结构 (0)
- python (17)
- redis (1)
- sql (2)
- ibatis (1)
- jquery (3)
- 测试 (3)
- linux (37)
- solr (3)
- oracle (5)
- jira (5)
- 版本控制 (3)
- xp (1)
- IDE (3)
- apache (4)
- hadoop (2)
- freemarker (2)
- maven (5)
- 项目管理 (2)
- UML (1)
- Django (6)
- 正则 (1)
- Scrapy (1)
- 文档管理 (3)
- 项目集成 (8)
- MQ (3)
- 架构 (1)
- HTML (1)
- IT (1)
- 云 (0)
- 应用服务器 (4)
- win 7 (1)
- thrift (1)
- 学习 (3)
- OpenStack (3)
- sqlserver (1)
- javascript (1)
- zabbix (3)
- IOS (1)
- rabbitmq (1)
- springcloud (2)
最新评论
-
xushenkun4:
至今仍然有这个bug,0.9.1无法传输中文utf8。
thrift使用出现诡异问题 -
feiniao2029:
[i][/i][u][/u]引用
spring 配置init方法 -
wt811004:
非常感谢朋友慷慨指导
dotproject项目管理工具使用 -
hackpro:
这将是一个经典,就像大话西游...
javaeye怀旧 -
raymond2006k:
个人更偏好 Velocity。我觉得还有个对比点,就是编程方式 ...
jsp freemarker velocity 比较
转载 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的默认分页功能是基于内置的表关联模型的,所以,如果我们有更多复杂的要求,比如:
联合两张表的查询分页
这样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 + crul 获得url的返回的文件流
2010-09-15 11:27 10041.获得url的文件流 function _cu ... -
cakephp file components
2010-09-13 12:56 11451.components文件file_helper.php & ... -
cakephp + jira soap client的使用
2010-09-13 11:29 16021.创建components文件jira_cl ... -
Cakephp + Predis使用文档
2010-09-13 11:17 1983Cakephp + Predis使用文档 1. 环境准备 ... -
cakephp之oracle <摘录>
2010-09-10 12:49 3237环境:XP系统+apache2.2+php5.3(5.2)+o ... -
cakephp之Ajax异步文件上传<摘录>
2010-08-17 15:24 2067why? 同一个需要填写信息和上传文件,一步步保存避免信息丢 ... -
cakephp如何在controller中获取分页信息
2010-07-15 15:15 1523controller的paginate方法用于获取符合条件的 ... -
cakephp jquery ajax json
2010-07-13 10:59 2044简介 JSON(JavaScript Object Nota ... -
Cakephp 开发随笔<一>
2010-07-13 10:42 11161.home页 在views/pages/下添加home. ... -
CakePHP log(日志)操作(摘录)
2010-07-09 11:06 1183CakePHP 的日志文件放置在目录 app/tmp/logs ... -
Cakephp 执行主要流程
2010-07-09 11:03 925加载基本文件 cake/basics.php 里面定义了常用的 ...
相关推荐
总的来说,CakePHP的PageHelper是实现分页功能的强大工具,它简化了分页的实现,提高了开发效率,并且允许开发者通过配置和自定义方法来满足各种复杂的分页需求。通过学习和熟练掌握PageHelper的使用,你可以创建更...
cakephp分页,cakephp框架的分页的方法的文档,是cakephp框架的内容。
2:cakephp中虽然对模型的操作方法中并没有提供像上面提到的连贯方法 但是cake框架提供了一个containable行为类来更好的完善模型关联中遇到的上述问题 ">1:thinkphp中通过框架提供的连贯操作Model >join ‘other ...
在CakePHP中,模型通常与数据库表关联,用于执行CRUD(创建、读取、更新、删除)操作。ORM(对象关系映射)是模型层的核心,使得开发者可以用面向对象的方式来操作数据库。 4. **视图(View)**: 视图负责展示...
在CakePHP中,这通常通过在`find`方法或`paginate`方法中指定`joins`参数来实现。例如,这里的`paginate`配置中,有三个JOIN操作: 1. `UserInfo`表通过`USERID`字段与`LoginHistory`表关联,定义为LEFT JOIN,意味...
CakePHP 的路由系统允许开发者自定义URL结构,使得URL更符合人类阅读习惯。通过配置路由规则,可以将任意URL映射到特定的控制器和动作。 **表单验证(Validation)** 在模型层,CakePHP 提供了强大的表单验证机制。...
2. **查询构造器**:通过ORM(对象关系映射),开发者可以使用链式方法构建复杂的SQL查询,无需直接编写SQL语句。 ### 四、验证和错误处理 1. **数据验证**:在模型层,可以设置数据验证规则,确保输入数据的完整...
5. **社区支持**:由于有庞大的社区支持,开发者可以找到许多插件和教程来增强功能或解决遇到的问题。 6. **错误和日志管理**:CakePHP 提供了强大的错误报告和日志记录功能,帮助开发者追踪和调试问题。 7. **...
在CakePHP中,ACL由三个主要部分组成:Aro(Access Request Object),Aco(Access Control Object)和Ace(Access Control Entry)。Aro代表请求访问的主体,如用户或角色;Aco代表可被访问的客体,如控制器、动作...
全面的cakephp中文文档
在CakePHP中,Access Control List(ACL)是一个强大的工具,用于管理应用程序中的权限和访问控制。让我们深入探讨一下CakePHP中的ACL系统。 ### ACL基础概念 **1. ACO (Access Control Object)**:ACO代表要控制...
CakePHP有多个特点,这些特点让CakePHP成为了快速开发框架中的佼佼者之一。 1.活跃友好的社区 2.灵活的许可协议(Licensing) 3.兼容PHP4和PHP5 4.数据库交互和简单查询的集成 5.应用程序Scaffolding 6....
你可以通过升级 CakePHP 版本来更新这些文件,而不用担心覆盖自定义的应用程序代码。 3. **vendors**:这个目录用于存放第三方库或组件。通过`vendor()`方法可以方便地调用这些外部库。 完整的目录结构还包含其他...
CakePHP中文手册
CakePHP的分页组件源码;CakePHP的中文及英文教程,CHM格式;CakePHP的中文打印版教程,WORD格式,下载后可直接打印,方便的;CakePHP的HTML格式的教程;CakePHP的使用技巧介绍;CakePHP开发的网站源码参考;CakePHP...
10. **调试与性能优化**:学习如何使用 CakePHP 的调试模式进行问题排查,以及如何进行性能优化,如使用缓存和优化查询。 11. **插件(Plugins)**:CakePHP 插件允许你重用和扩展框架的功能,手册会解释如何安装、...
CakePHP的分页组件源码;CakePHP的中文及英文教程,CHM格式;CakePHP的中文打印版教程,WORD格式,下载后可直接打印,方便的;CakePHP的HTML格式的教程;CakePHP的使用技巧介绍;CakePHP开发的网站源码参考;CakePHP...
在CakePHP中,MVC的实现十分直观。例如: - **模型(Model)**:开发者可以创建对应的数据库表类,继承自CakePHP的`AppModel`,用于操作数据库。模型类提供了数据查询和修改的方法,如`find()`和`save()`。 - **...