Having query with group by and limit:
$query = Products::find()->joinWith('units')->groupBy('id')->limit(10);
Total count of all rows
Total count of limited rows (10)
COUNT query is:
SELECT COUNT(*) FROM
(SELECT `products`.* FROM `products`
LEFT JOIN `units` `u` ON `products`.`unit_id` = `u`.`id
GROUP BY `products`.`id` LIMIT 1
) `c`
instead of
SELECT COUNT(*) FROM
(SELECT `products`.* FROM `products`
LEFT JOIN `units` `u` ON `products`.`unit_id` = `u`.`id
GROUP BY `products`.`id`
) `c`
Related issue where code was created: https://github.com/yiisoft/yii2/issues/2490
I think you just need to execute query first and then re-assign select, limit and offset
This is expected result.
You should not set limit for the count query or unset it before like it is done at ActiveDataProvider:
https://github.com/yiisoft/yii2/blob/master/framework/data/ActiveDataProvider.php#L165
facepalm.jpg
if (empty($this->groupBy) && empty($this->having) && empty($this->union) && !$this->distinct) {
return $command->queryScalar();
but I have no idea why you don't want to do the same for the second part.
Most helpful comment
This is expected result.
You should not set limit for the count query or unset it before like it is done at
ActiveDataProvider:https://github.com/yiisoft/yii2/blob/master/framework/data/ActiveDataProvider.php#L165