我有一个非常复杂的查询CSqlDataProvider
,我试图让分页totalItemCount
工作,但它返回错误的计数。我试图为 COUNT 个项目创建第二个 SQL,但这真的很难,有人可以帮助我吗?
CSqlDataProvider 文档:http ://www.yiiframework.com/doc/api/1.1/CSqlDataProvider
控制器代码:
$dayofweek = date('w');
$sql = "( SELECT `profile`.`id` as profile_id, `profile`.`name` as profile_name, `events_reminder`.`id`,
CASE WHEN weekday >= $dayofweek
THEN
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
ELSE
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
END as revent_datetime,
CASE WHEN weekday >= $dayofweek
THEN
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
ELSE
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
END as rlist_datetime
FROM events_reminder
LEFT OUTER JOIN `profiles` `profile` ON (`events_reminder`.`profile_id`=`profile`.`id`)
HAVING revent_datetime NOT IN (SELECT DATE_FORMAT(event_datetime, '%d/%m/%Y %H:%i') FROM events WHERE `events`.`profile_id`=`events_reminder`.`profile_id`) )
UNION ALL
( SELECT `profile`.`id` as profile_id, `profile`.`name` as profile_name, `events_reminder`.`id`,
CASE WHEN weekday >= $dayofweek
THEN
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek + 7 DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
ELSE
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday + 7 DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
END as revent_datetime,
CASE WHEN weekday >= $dayofweek
THEN
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek + 7 DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
ELSE
DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday + 7 DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
END as rlist_datetime
FROM events_reminder
LEFT OUTER JOIN `profiles` `profile` ON (`events_reminder`.`profile_id`=`profile`.`id`)
HAVING revent_datetime NOT IN (SELECT DATE_FORMAT(event_datetime, '%d/%m/%Y %H:%i') FROM events WHERE `events`.`profile_id`=`events_reminder`.`profile_id`) )
ORDER BY revent_datetime";
$count=Yii::app()->db->createCommand($sql)->queryScalar();
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));