douyalin2258 2012-04-02 12:44
浏览 18
已采纳

优化Code​​Igniter中的活动记录

I have a very complicated funciton in one of my CI models so I try to optimise it and make it more robust.I'm not sure where exactly the problem is so I'll paste both the original(working) variant and what I have done to make it better. I think in the most part it should work but obv. I've made a mistake somewhere along the way.

Here is the original function:

public function get($data)
    {
        if (isset($data))
        {
            if (isset($data['sort']))
            {
                $sort = json_decode($data['sort'], true);
                $this->db->order_by($sort[0]['property'], $sort[0]['direction']);
            }

            if (isset($data['query']) && $data['query'] != '')
            {
                $fields = json_decode($data['fields'], true);
                $where = $fields[0] . " LIKE '%" . $data['query'] . "%'";
                unset($fields[0]);
                foreach ($fields as $field)
                {
                    $where .= ' OR ' . $field . ' LIKE ' . "'%" . $data['query'] . "%'";
                }
                $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
                $this->db->from('users');
                $this->db->where($where);
                $this->db->limit($data['limit'], $data['start']);
                $query = $this->db->get();
                $result = $query->result_array();
            }
            else
            {
                $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
                $this->db->from('users');
                $this->db->limit($data['limit'], $data['start']);
                $query = $this->db->get();
                $result = $query->result_array();
            }

            if ($result != null)
            {
                return $result;
            }
            else
            {
                return null;
            }
        }
        else
        {
            $query = $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
            $query = $this->db->get('users');
            $result = $query->result_array();
            return $result;
        }
    }

And here is what I've done:

public function get($data)
    {

        if (isset($data))
        {
            if (isset($data['sort']))
            {
                $sort = json_decode($data['sort'], true);
                $orderCoulmn = $sort[0]['property'];
                $orderDir = $sort[0]['direction'];
            }

        $limit = $data['limit'];

        $start = $data['start'];

        }

        $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');

    /*  if (!empty($where))
        {
            $this->db->where($where);
        }*/
        if (isset($data['query']) && $data['query'] != '' )
        {
            $fields = json_decode($data['fields'], true);
            //$this->db->like($fields[0], $data['query']);
            //unset($fields[0]);

            foreach ($fields as $filed)
            {
                $this->db->or_like($field, $data['query']);
            }
        }

        if (!empty($limit) && !empty($start))
        {
            $this->db->limit($limit, $start);
        }

        if (!empty($orderColumn) && !empty($orderDir))
        {
            $this->db->order_by($orderColumn, $orderDir);
        }

        $query = $this->db->get('users');
        $result = $query->result_array();

        return $result;
    }

Any ideas where could be the problem in my code (the second one)? Thanks Leron

  • 写回答

1条回答 默认 最新

  • duanliang8464 2012-04-02 14:32
    关注

    I don't directly see what's wrong with your example, but this is how I would simplify things:

    public function get($data)
    {
        $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position'); // same for every situation
    
        if (is_array($data)) // a little stricter than testing if it's set, could be a string.
        {
            $this->db->limit($data['limit'], $data['start']); // do anyhow if $data is an array
    
            if ($data['sort']) // if this evaluates to true, execute
            {
                $sort = json_decode($data['sort'], true);
                $this->db->order_by($sort[0]['property'], $sort[0]['direction']);
            }
    
            if ($data['query'])
            {
                $fields = json_decode($data['fields'], true);
                $where = "";
                $seperator = "";
                foreach($fields as $field)
                {
                    $where .= "{$seperator}$field LIKE '%{$data['query']}%'";
                    $seperator = ' OR '; // using this "seperator" approach, allows you to easily concat strings.
                }
                $this->db->where($where);
            }
        }
    
        // finally, get a result, with possible other db actions, depending on $data
        $query = $this->db->get('users');
        $result = $query->result_array();
        return $result;
    }
    

    Remember: KISS and DRY are your friends ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思