2012-04-02 8 views
1

私はCIモデルの中で非常に複雑な機能を持っていますので、最適化してより頑強にしようとしています。どこに問題があるのか​​わからないので、元の私はそれをより良くするために何をしましたか?私はほとんどの部分で動作するはずだが、obvだと思う。私は途中で間違いを犯しました。ここでCodeIgniterでアクティブレコードを最適化する

は本来の機能である:

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; 
     } 
    } 

そして、ここでは、私がやったことです:

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; 
    } 

私のコードに問題がある可能性があります任意のアイデア(1秒)? Leron

+1

場合あなたはコードを介して私たちに話しかけるか、またはあなたが手助けするのが簡単かもしれないことをあなたに伝えてください。 – Tobias

+0

まあ、私は正確に何を言いたいのかわからない。それは働くプログラムからの働く機能の部分です。私の教えからの部分はコードを最適化することです。言い換えれば、それをより良い形で書き直すことです。元のファイルを見て、今私が何をしているのかを見ることができます。 $ dataは、検索メニューから投稿された値を持つ変数です... – Leron

答えて

0

おかげで私が直接あなたの例と間違って何が表示されないが、これは、私は物事を単純化する方法を示します。

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; 
} 

は覚えている:KISSとDRYはあなたの友達です;)

関連する問題