【问题标题】:Codeigniter count_all_results with havingCodeigniter count_all_results 具有
【发布时间】:2019-01-04 17:15:27
【问题描述】:

我使用 Codeigniter 的 Query Builder 类编写了一个查询。该查询使用别名和 having 方法。当我对此查询调用 count_all_results 方法时,会发生异常。检查日志,我看到查询已经删除了“有”子句。有没有办法在调用 count_all_results 时保留这些子句?感谢您的帮助。

编辑:我首先认为问题是基于知识而不是基于代码的,因此没有共享代码,但在这里。如果需要更多信息,请告诉我。

这是对控制器中模型的调用。

$where_array = array(
    $parent_key.' is not NULL' => null
);
$search_post = $request_data['search'];
if (isset($request_data['filter'])) {
    $filter_array = $request_data['filter'];
    foreach ($filter_array as $filter_pair) {
        if (isset($filter_pair['escape'])) {
            $where_array[$filter_pair['filterBy']] = null;
        } else {
            if ($filter_pair['filterBy'] == 'table3_id') {
            $where_array['table3.'.$filter_pair['filterBy']] = isset($filter_pair['filterId']) ?
                $filter_pair['filterId'] : null;
            } else {
                $where_array[$table.'.'.$filter_pair['filterBy']] = isset($filter_pair['filterId']) ?
                    $filter_pair['filterId'] : null;
            }
        }
    }
}
$like_array = array();
foreach ($request_data['columns'] as $key => $column) {
    if (!empty($column['search']['value'])) {
        $like_array[$column['data']] = $column['search']['value'];
    }
}
$totalFiltered = $this->$model_name->modelSearchCount($search, $where_array, $like_array);

这是模型方法。

public function modelSearchCount($search, $where_array = null, $like_array = null)
{
    $this->joinLookups(null, $search);
    if ($where_array) {
        $this->db->where($where_array);
    }
    if ($like_array) {
        foreach($like_array as $key => $value) {
            $this->db->having($key." LIKE '%". $value. "%'");
        }
    }
    return $this->db->from($this->table)->count_all_results();
}

protected function joinLookups($display_config = null, $search = null)
{
    $select_array = null;
    $join_array = array();
    $search_column_array = $search ? array() : null;
    $i = 'a';

    $config = $display_config ? $display_config : $this->getIndexConfig();
    foreach ($config as $column) {
        if (array_key_exists($column['field'], $this->lookups)) {
            $guest_model_name = $this->lookups[$column['field']];
            $this->load->model($guest_model_name);
            $join_string =$this->table.'.'.$column['field'].'='.$i.'.'.
                $this->$guest_model_name->getKey();
            $guest_display = $this->$guest_model_name->getDisplay();
            if ($search) {
                $search_column_array[] = $i.'.'.$guest_display;
            }
            $join_array[$this->$guest_model_name->getTable().' as '.$i] = $join_string;
            $select_array[] = $i.'.'.
                $guest_display;
        } else {
            $select_array[] = $this->table.'.'.$column['field'];
            if ($search) {
                $search_column_array[] = $this->table.'.'.$column['field'];
            }
        }
        $i++;
    }
    $select_array[] = $this->table.'.'.$this->key;
    foreach ($join_array as $key => $value) {
        $this->db->join($key, $value, 'LEFT');
    }
    $this->db->join('table2', $this->table.'.table2_id=table2.table2_id', 'LEFT')
        ->join('table3', 'table2.table3_id=table3.table3_id', 'LEFT')
        ->join('table4', $this->table.'.table4_id=table4_id', 'LEFT')
        ->join('table5', 'table4.table5_id=table5.table5_id', 'LEFT');
    $this->db->select(implode($select_array, ', '));
    if ($search) {
        foreach (explode(' ', $search) as $term) {
            $this->db->group_start();
                $this->db->or_like($this->table.'.'.$this->key, $term);
            foreach ($search_column_array as $search_column) {
                $this->db->or_like($search_column, $term);
            }
            $this->db->group_end();
        }
    }
    $this->db->select('table2_date, '. $this->table.'.table2_id, table4_id, '. 'table5.table5_description');
}

【问题讨论】:

  • 你需要显示一些代码(至少来自你的模型),显示你得到的特定异常等等......否则几乎不可能帮助你
  • 如果提供的code不足请告知,谢谢。
  • @JavierLarroulet 看到我的编辑,谢谢。
  • 嗨@Enoch,这将有助于查看模型中构建的整个查询,而不仅仅是构建having 子句的块。如果相关,从控制器传递的参数也会有所帮助。您收到的错误消息也可能有助于查明问题。请记住,您需要提供Minimal, complete and verifiable example
  • @JavierLarroulet 我已经包含了模型中出现的整个查询构建,以及控制器中模型调用的前导。真的我的问题是为什么 count_all_results 从它的查询中去掉“有”,以及我怎样才能让它不这样做。

标签: codeigniter codeigniter-query-builder


【解决方案1】:

由于count_all_results() 基本上会运行Select count(*) 并且不计算结果集中的行数(基本上使查询对您的目的无用),因此您可以使用其他 Codeigniter 方法来获取结果集和行数。

尝试将查询运行到变量中:

 $query = $this->db->get();

从那时起,您几乎可以做任何事情。除了使用$query->result(); 返回结果之外,您还可以使用以下命令将行数放入另一个变量中:

 $rownum = $query->num_rows();

然后您可以将其返回到您的控制器中,甚至只返回 $query 对象,然后在控制器上运行 num_rows() 方法

【讨论】:

    【解决方案2】:

    为了回答这个问题,count_all_results() 通过将您的选择替换为 SELECT COUNT(*) FROM table 来转换原始查询。别名列将不会被选中,have 子句将无法识别该列。这就是 count_all_results() 不能与 have 一起使用的原因。

    【讨论】:

    • 好发现...我还是没弄明白。使用 CI 方法的一种解决方法是将查询运行到变量中(例如$result = $this->db->get();),然后使用另一种方法来获取结果行数(例如$numrows = $result->num_rows();)......让我知道这是否适用你这样我就可以像答案一样发布它
    • @JavierLarroulet 是的,您的解决方案似乎是在查询中保留子句的唯一方法。谢谢。
    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多