代码之家  ›  专栏  ›  技术社区  ›  Enoch

编码器点火器计数

  •  -1
  • Enoch  · 技术社区  · 7 年前

    我已经使用CodeIgniter的查询生成器类编写了一个查询。查询使用别名和HAVING方法。当我在此查询上调用count_all_results方法时,会发生异常。检查日志后,我发现查询已经去掉了“having”子句。在调用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');
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Javier Larroulet    7 年前

    自从 count_all_results() 基本上会运行一个 Select count(*) 并且不计算结果集中的行数(基本上使查询对您的目的毫无用处),您可以使用其他CodeIgniter方法来获取结果集和行数。

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

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

    从那时起,你几乎可以做任何事。除了返回结果 $query->result(); 您可以使用以下命令将行数转换为另一个变量:

     $rownum = $query->num_rows();
    

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

        2
  •  0
  •   Enoch    7 年前

    若要回答此问题,count_all_results()将原始查询替换为 从中选择count(*) table . 不会选择别名列,HAVING子句也无法识别该列。这就是为什么count_all_results()不适用于having。

    推荐文章