简体   繁体   English

Grocery_CRUD where子句不能与过滤器一起使用

[英]Grocery_CRUD where clause does not work together with filters

In the project in which I fixed errors, was used grocery_CRUD () plugin. 在我修复了错误的项目中,使用了杂货店的CRUD()插件。 The documentation has an example of using the where clause as below: 该文档提供了一个使用where子句的示例,如下所示:

$crud = new grocery_CRUD();
$crud->set_language($this->config->item('language'));  
$crud->set_table('users_products');      
$crud->where('users_products.users_id !=', 1);

The table does not contain records that do not meet the conditions. 该表不包含不符合条件的记录。 It looks like everything is ok. 看起来一切正常。 Unfortunately, when the user uses the filters placed under the plugin, records that do not meet the condition are also included in the search results. 不幸的是,当用户使用置于插件下方的过滤器时,不符合条件的记录也将包含在搜索结果中。

What am I doing wrong? 我究竟做错了什么? How do I add search terms? 如何添加搜索词?

Ok, I found a solution to the problem. 好的,我找到了解决问题的办法。 Maybe it is not perfect and probably needs some refactoring but everyone I think will understand the principle of this solution: 也许它不是完美的,可能需要进行一些重构,但是我认为每个人都会理解此解决方案的原理:

In /application/models/Grocery_crud_model.php we must to add two functions: 在/application/models/Grocery_crud_model.php中,我们必须添加两个功能:

    function group_start()
    {
        $this->db->group_start();
    }
    function group_end()
    {
        $this->db->group_end();
    }

In /application/libraries/Grocery_CRUD.php we add: 在/application/libraries/Grocery_CRUD.php中,我们添加:

    public function whereFix($key, $value = NULL, $escape = TRUE)
    {
        $this->whereFix[] = array($key,$value,$escape);

        return $this;
    }

In the same file we add loop to create whereFix conditions and closure for another types of terms by group_start() and group_end(). 在同一文件中,我们添加循环以通过group_start()和group_end()创建条件修复条件并关闭其他类型的术语。 In fact we can replace functions get_list() and get_total_results(): 实际上,我们可以替换函数get_list()和get_total_results():

    protected function get_list()
    {
        if(!empty($this->whereFix))
            foreach($this->whereFix as $where)
                $this->basic_model->where($where[0],$where[1],$where[2]);

        if(!empty($this->order_by))
            $this->basic_model->order_by($this->order_by[0],$this->order_by[1]);


        if(!empty($this->where)){
                    $this->basic_model->group_start();
            foreach($this->where as $where)
                $this->basic_model->where($where[0],$where[1],$where[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->or_where)){
                    $this->basic_model->group_start();
            foreach($this->or_where as $or_where)
                $this->basic_model->or_where($or_where[0],$or_where[1],$or_where[2]);
                    $this->basic_model->group_end();
                }        

        if(!empty($this->like)){
                    $this->basic_model->group_start();
            foreach($this->like as $like)
                $this->basic_model->like($like[0],$like[1],$like[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->or_like)){
                    $this->basic_model->group_start();
            foreach($this->or_like as $or_like)
                $this->basic_model->or_like($or_like[0],$or_like[1],$or_like[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->having))
            foreach($this->having as $having)
                $this->basic_model->having($having[0],$having[1],$having[2]);

        if(!empty($this->or_having))
            foreach($this->or_having as $or_having)
                $this->basic_model->or_having($or_having[0],$or_having[1],$or_having[2]);

        if(!empty($this->relation))
            foreach($this->relation as $relation)
                $this->basic_model->join_relation($relation[0],$relation[1],$relation[2]);

        if(!empty($this->relation_n_n))
        {
            $columns = $this->get_columns();
            foreach($columns as $column)
            {
                //Use the relation_n_n ONLY if the column is called . The set_relation_n_n are slow and it will make the table slower without any reason as we don't need those queries.
                if(isset($this->relation_n_n[$column->field_name]))
                {
                    $this->basic_model->set_relation_n_n_field($this->relation_n_n[$column->field_name]);
                }
            }

        }

        if($this->theme_config['crud_paging'] === true)
        {
            if($this->limit === null)
            {
                $default_per_page = $this->config->default_per_page;
                if(is_numeric($default_per_page) && $default_per_page >1)
                {
                    $this->basic_model->limit($default_per_page);
                }
                else
                {
                    $this->basic_model->limit(10);
                }
            }
            else
            {
                $this->basic_model->limit($this->limit[0],$this->limit[1]);
            }
        }

        $results = $this->basic_model->get_list();

        return $results;
    }
    protected function get_total_results()
    {
        if(!empty($this->whereFix))
            foreach($this->whereFix as $where)
                $this->basic_model->where($where[0],$where[1],$where[2]);

                if(!empty($this->where)){
                    $this->basic_model->group_start();
            foreach($this->where as $where)
                $this->basic_model->where($where[0],$where[1],$where[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->or_where)){
                    $this->basic_model->group_start();
            foreach($this->or_where as $or_where)
                $this->basic_model->or_where($or_where[0],$or_where[1],$or_where[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->like)){
                    $this->basic_model->group_start();
            foreach($this->like as $like)
                $this->basic_model->like($like[0],$like[1],$like[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->or_like)){
                    $this->basic_model->group_start();
            foreach($this->or_like as $or_like)
                $this->basic_model->or_like($or_like[0],$or_like[1],$or_like[2]);
                    $this->basic_model->group_end();
                }

        if(!empty($this->having))
            foreach($this->having as $having)
                $this->basic_model->having($having[0],$having[1],$having[2]);

        if(!empty($this->or_having))
            foreach($this->or_having as $or_having)
                $this->basic_model->or_having($or_having[0],$or_having[1],$or_having[2]);

        if(!empty($this->relation))
            foreach($this->relation as $relation)
                $this->basic_model->join_relation($relation[0],$relation[1],$relation[2]);

        if(!empty($this->relation_n_n))
        {
            $columns = $this->get_columns();
            foreach($columns as $column)
            {
                //Use the relation_n_n ONLY if the column is called . The set_relation_n_n are slow and it will make the table slower without any reason as we don't need those queries.
                if(isset($this->relation_n_n[$column->field_name]))
                {
                    $this->basic_model->set_relation_n_n_field($this->relation_n_n[$column->field_name]);
                }
            }

        }

        return $this->basic_model->get_total_results();
    }

I hope this piece of code will be useful for someone. 我希望这段代码对某人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM