数据映射器不返回total_rows

时间:2012-08-14 07:04:30

标签: codeigniter grouping datamapper

$b->include_related('tent',NULL,TRUE,TRUE)->include_related('booking/guest',NULL,TRUE,FALSE)->include_related('booking/bookingbasis',NULL,TRUE,FALSE)->include_related('booking',NULL,TRUE,FALSE);
            $b->group_by('booking_id');
            $b->select_sum('booking_total','sum_booking_booking_total');
            $b->select_sum('booking_taxes','sum_booking_booking_taxes');
            $b->get_paged(1,1);
            $total = $b->paged->total_rows;

共有2行。 但$ total总共只返回1.

其他的事情是,如果我删除以下代码,它返回2.从分组开始的问题。

$b->group_by('booking_id');
                $b->select_sum('booking_total','sum_booking_booking_total');
                $b->select_sum('booking_taxes','sum_booking_booking_taxes');

任何人都可以猜出是什么问题。

1 个答案:

答案 0 :(得分:0)

我认为它是一个旧帖子,但我面临同样的问题,并认为我至少可以提供一些意见。

如上所述,问题在于' group_by'。这是因为总结果的计数超出了由于group_by而产生的组。

要获得所需的总结果,您应该执行DISTINCT选择。但是,这将导致返回的行集出现问题。

因此,问题在于您要使用DISTINCT获取正确的分页 - > total_rows,而使用GROUP_BY来获取正确的对象集。

可能的解决方案

  • 打开datamapper.php库
  • 搜索'公共功能get_paged'
  • 使该函数的行为方式使groupby转换为总数的不同选择。

替换

$total = $count_query->db->ar_distinct ? $count_query->count_distinct() : $count_query->count();

使用

$distinct_column = 'id';
if($count_query->db->ar_groupby){
    $distinct_column = implode(",",$count_query->db->ar_groupby);
    $count_query->db->ar_distinct = TRUE;
    $count_query->db->ar_groupby = NULL;
}
$total = $count_query->db->ar_distinct ? $count_query->count_distinct(NULL, $distinct_column) : $count_query->count();

这样,groupby-field将在distinct-count中使用,并设置为NULL以在total-results中被忽略。