ITeye博客

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

zhb1208 · 2010-07-16 09:31 · cakephp · 编程语言 · PHP · Flash · SQL · CakePHP · Markdown

转载 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不会自动格式化参数。

  1. 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 ?

  1. class SavedNote extends AppModel {
  2.  var $name = 'SavedNote' ;
    
  3.  //这个模型不使用任何表,它仅仅为我们提供自定义分页支持
    
  4.  var $useTable = false;
    
  5.  var $user_id = 0;
    
  6.  /*
    
  7.   * Paginate Function
    
  8.  */
    
  9.  function paginate( $conditions , $fields , $order , $limit , $page = 1, $recursive = null) {
    
  10.      $offset = ((int) $page - 1) * (int) $limit ;
    
  11.      $conditions = "
    
  12.      //这里省略的SQL会在附录中列出
    
  13.      ";
    
  14.      return $this ->query( $conditions );
    
  15.  }
    
  16.  /*
    
  17.   * Paginate Count Function
    
  18.  */
    
  19.  function paginateCount( $conditions = null, $recursive = 0) {
    
  20.      $conditions = "
    
  21.      //这里省略的SQL会在附录中列出
    
  22.      ";
    
  23.      $result = $this ->query( $conditions );
    
  24.      return (int) $result [0][0][ 'NUMS' ];
    
  25.  }
    
  26. }
  27. ?>

我们可以在控制器中使用这个自定义查询了: 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 ?

  1. class NotesController extends AppController{
  2.  var $name = 'Notes' ;
    
  3.  var $layout = 'notes' ;
    
  4.  //这里引入我们需要的模型
    
  5.  var $uses = array ( 'Note' , 'SavedNote' , 'TrashNote' );
    
  6.  function saved(){
    
  7.      $user_id = $this ->Auth->User( 'id' );
    
  8.      $limit = 5;
    
  9.      //这里为我们的自定义查询,传递一些参数
    
  10.      $this ->paginate = array (
    
  11.          'SavedNote' => compact( 'limit' )
    
  12.      );
    
  13.      $this ->SavedNote->user_id = $user_id ;
    
  14.      //控制器的paginate方法,会自定寻找模型的相应方法,并调用
    
  15.      $notes = $this ->paginate( 'SavedNote' );
    
  16.      $this ->set(compact( 'notes' ));
    
  17.  }
    
  18. }

最后,希望大家看到的是,自定义模型中的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 ?

  2. SELECT

  3.  DISTINCT (SavedNote.note_id),
    
  4.  SavedNote.*,
    
  5.  Note.subject,
    
  6.  Note.body,
    
  7.  Note.created,
    
  8.  SentUser. name ,
    
  9.  ReceivedUser. name
    
  10. FROM (

  11.  SELECT
    
  12.      ReceivedNote.note_id AS note_id,
    
  13.      ReceivedNote.receive_user_id AS receive_user_id,
    
  14.      ReceivedNote.send_user_id AS send_user_id,
    
  15.      ReceivedNote.is_deleted AS is_deleted,
    
  16.      ReceivedNote.is_saved AS is_saved,
    
  17.      ReceivedNote.read_date AS read_date,
    
  18.      'received' as src
    
  19.  FROM
    
  20.      received_notes AS ReceivedNote
    
  21.  WHERE
    
  22.      receive_user_id = {$this->user_id}
    
  23.  UNION ALL
    
  24.  SELECT
    
  25.      SentNote.note_id AS note_id,
    
  26.      SentNote.receive_user_id AS receive_user_id,
    
  27.      SentNote.send_user_id AS send_user_id,
    
  28.      SentNote.is_deleted AS is_deleted,
    
  29.      SentNote.is_saved AS is_deleted,
    
  30.      SentNote.read_date AS read_date,
    
  31.      'sent' as src
    
  32.  FROM
    
  33.      sent_notes AS SentNote
    
  34.  WHERE
    
  35.      send_user_id = {$this->user_id}
    
  36. ) as SavedNote

  37. LEFT JOIN notes AS Note ON Note.id = SavedNote.note_id

  38. LEFT JOIN users AS SentUser ON SentUser.id = SavedNote.send_user_id

  39. LEFT JOIN users AS ReceivedUser ON ReceivedUser.id = SavedNote.receive_user_id

  40. WHERE

  41. SavedNote.is_saved = 1

  42. ORDER BY

  43. Note.created DESC

  44. LIMIT {$offset},{$limit}

  45. 模型中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 ?

  46. SELECT

  47.  COUNT ( DISTINCT (SavedNote.note_id)) AS NUMS
    
  48. FROM (

  49.  SELECT
    
  50.      ReceivedNote.note_id AS note_id,
    
  51.      ReceivedNote.receive_user_id AS receive_user_id,
    
  52.      ReceivedNote.send_user_id AS send_user_id,
    
  53.      ReceivedNote.is_saved AS is_saved,
    
  54.      'received' as src
    
  55.  FROM
    
  56.      received_notes AS ReceivedNote
    
  57.  WHERE
    
  58.      receive_user_id = {$this->user_id}
    
  59.  UNION ALL
    
  60.  SELECT
    
  61.      SentNote.note_id AS note_id,
    
  62.      SentNote.receive_user_id AS receive_user_id,
    
  63.      SentNote.send_user_id AS send_user_id,
    
  64.      SentNote.is_saved AS is_saved,
    
  65.      'received' as src
    
  66.  FROM
    
  67.      sent_notes AS SentNote
    
  68.  WHERE
    
  69.      send_user_id = {$this->user_id}
    
  70. ) as SavedNote

  71. WHERE

  72. SavedNote.is_saved = 1

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。