BLOG ENTRY

CakePHPのpaginateでJOIN [CakePHP]

cakephp

CakePHPのPaginate(ページ送り機能)でjoinを使う場合です。

とりあえずMySQL内のテーブルは
users, works, messagesの3つでLEFT JOINのケース

コントローラのメソッド内でこんな感じで指定してやると。

[php]
$this->paginate = array(
‘User’ => array(
‘conditions’ => array(),
‘limit’ => 24,
‘order’ => array(‘User.user_id’ => ‘desc’),
‘joins’ => array(
array(‘type’ => ‘LEFT’, ‘alias’ => ‘Work’, ‘table’ => ‘works’,
‘conditions’ => ‘User.user_id = Work.user_id’)
array(‘type’ => ‘LEFT’, ‘alias’ => ‘Message’, ‘table’ => ‘messages’,
‘conditions’ => ‘Work.user_id = Message.user_id’)
))
);
[/php]

こんなSQLになります

[sql]
SELECT
取得カラム名
FROM
`users` AS `User`
LEFT JOIN
works AS `Work`
ON
(`User`.`user_id`=`Work`.`user_id`)
LEFT JOIN
messages AS `Message`
ON
(`Work`.`user_id`=`Message`.`user_id`)
ORDER BY
`User`.`user_id` desc LIMIT 24
[/sql]

  • name:ほおく
  • date:2010/04/10
  • あなたは神だ!!

  • type:コメント [ ]

    WRITE COMMENT


    (required)


    (required)


    (required)

    MENU

    veltica creative of twitter