Cakephp中自定义查询的分页问题,解决方法
转载 Cakephp中自定义查询的分页问题,解决方法 收藏
Cakephp的默认分页功能是基于内置的表关联模型的,所以,如果我们有更多复杂的要求,比如:
联合两张表的查询分页
这样Cakephp的默认分页就达不到我们的要求,解决的方法是:
使用自定义的查询分页
使用自定义查询分页需要在模型中重载两个方法,paginate()和paginateCount()
- paginate()
提供分页的数据支持,原型如下:
- function paginate( $conditions , $fields , $order , $limit , $page = 1, $recursive = null);
参数由Cakephp自定传递,使用方法和find方法是一样的,参数通过在Controller中定义paginate变量传递,例如:
- $limit = 5;
- $this ->paginate = array (
-
'SavedNote' => compact( 'limit' ) - );
注意:
如果重载paginate()的函数使用参数,请自己格式化参数为自己定义的格式,Cakephp不会自动格式化参数。
- paginateCount()
提供分页的数据数量统计支持,原型如下:
- 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 ?
- class SavedNote extends AppModel {
-
var $name = 'SavedNote' ; -
//这个模型不使用任何表,它仅仅为我们提供自定义分页支持 -
var $useTable = false; -
var $user_id = 0; -
/* -
* Paginate Function -
*/ -
function paginate( $conditions , $fields , $order , $limit , $page = 1, $recursive = null) { -
$offset = ((int) $page - 1) * (int) $limit ; -
$conditions = " -
//这里省略的SQL会在附录中列出 -
"; -
return $this ->query( $conditions ); -
} -
/* -
* Paginate Count Function -
*/ -
function paginateCount( $conditions = null, $recursive = 0) { -
$conditions = " -
//这里省略的SQL会在附录中列出 -
"; -
$result = $this ->query( $conditions ); -
return (int) $result [0][0][ 'NUMS' ]; -
} - }
- ?>
我们可以在控制器中使用这个自定义查询了: 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 ?
- class NotesController extends AppController{
-
var $name = 'Notes' ; -
var $layout = 'notes' ; -
//这里引入我们需要的模型 -
var $uses = array ( 'Note' , 'SavedNote' , 'TrashNote' ); -
function saved(){ -
$user_id = $this ->Auth->User( 'id' ); -
$limit = 5; -
//这里为我们的自定义查询,传递一些参数 -
$this ->paginate = array ( -
'SavedNote' => compact( 'limit' ) -
); -
$this ->SavedNote->user_id = $user_id ; -
//控制器的paginate方法,会自定寻找模型的相应方法,并调用 -
$notes = $this ->paginate( 'SavedNote' ); -
$this ->set(compact( 'notes' )); -
} - }
最后,希望大家看到的是,自定义模型中的paginate参数的使用是需要自己手动解析和分配的,如果要使用Cakephp的默认查询样式,还需要自己解析一下。
附录:
-
模型中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 ?
-
SELECT
-
DISTINCT (SavedNote.note_id), -
SavedNote.*, -
Note.subject, -
Note.body, -
Note.created, -
SentUser. name , -
ReceivedUser. name -
FROM (
-
SELECT -
ReceivedNote.note_id AS note_id, -
ReceivedNote.receive_user_id AS receive_user_id, -
ReceivedNote.send_user_id AS send_user_id, -
ReceivedNote.is_deleted AS is_deleted, -
ReceivedNote.is_saved AS is_saved, -
ReceivedNote.read_date AS read_date, -
'received' as src -
FROM -
received_notes AS ReceivedNote -
WHERE -
receive_user_id = {$this->user_id} -
UNION ALL -
SELECT -
SentNote.note_id AS note_id, -
SentNote.receive_user_id AS receive_user_id, -
SentNote.send_user_id AS send_user_id, -
SentNote.is_deleted AS is_deleted, -
SentNote.is_saved AS is_deleted, -
SentNote.read_date AS read_date, -
'sent' as src -
FROM -
sent_notes AS SentNote -
WHERE -
send_user_id = {$this->user_id} -
) as SavedNote
-
LEFT JOIN notes AS Note ON Note.id = SavedNote.note_id
-
LEFT JOIN users AS SentUser ON SentUser.id = SavedNote.send_user_id
-
LEFT JOIN users AS ReceivedUser ON ReceivedUser.id = SavedNote.receive_user_id
-
WHERE
-
SavedNote.is_saved = 1
-
ORDER BY
-
Note.created DESC
-
LIMIT {$offset},{$limit}
-
模型中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 ?
-
SELECT
-
COUNT ( DISTINCT (SavedNote.note_id)) AS NUMS -
FROM (
-
SELECT -
ReceivedNote.note_id AS note_id, -
ReceivedNote.receive_user_id AS receive_user_id, -
ReceivedNote.send_user_id AS send_user_id, -
ReceivedNote.is_saved AS is_saved, -
'received' as src -
FROM -
received_notes AS ReceivedNote -
WHERE -
receive_user_id = {$this->user_id} -
UNION ALL -
SELECT -
SentNote.note_id AS note_id, -
SentNote.receive_user_id AS receive_user_id, -
SentNote.send_user_id AS send_user_id, -
SentNote.is_saved AS is_saved, -
'received' as src -
FROM -
sent_notes AS SentNote -
WHERE -
send_user_id = {$this->user_id} -
) as SavedNote
-
WHERE
-
SavedNote.is_saved = 1