2011-02-26 54 views
2

该方法需要搜索搜索关键字和解析的mysql查询,并重写where表达式以包含LIKE%关键字%。这个方法应该分解成单独的方法吗?

它运作良好,但我不知道它好或坏的做法有这么多循环的方法...

private function build_where($query_array, $options) 
{ 
    //add WHERE starting point 
    $where = '';   

    if(!empty($query_array['WHERE'])) 
    { 
     //build where array 
     $where_array = $query_array['WHERE']; 

     //start the where 
     $where .= 'WHERE '; 

     //get columns array 
     $columns_array = $this->build_columns_array($query_array); 

     //if there is a search string   
     if(!empty($options['sSearch'])) 
     { 
      //check for enabled columns 
      $i = 0; 
      $columns_length = count($columns_array); 
      for($i; $i < intval($columns_length); $i++) 
      { 
       //create the options boolean array 
       $searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i]; 
      } 

      //loop through searchable_columns for true values 
      foreach($searchable_columns as $searchable_column_key => $searchable_column_val) 
      { 
       if($searchable_column_val == true) 
       { 
        //get an integer from the searchable_column key 
        $column_id = preg_replace("/[^0-9]/", '', $searchable_column_key); 

        //lookup column name by index 
        foreach($columns_array as $columns_array_key => $columns_array_val) 
        { 
         //if the $columns_array_key matches the $column_id 
         if($columns_array_key == $column_id) 
         { 
          //loop to build where foreach base expression 
          $i = 0; 
          $where_length = count($where_array); 
          for($i; $i < intval($where_length); $i++) 
          { 
           //append the existing WHERE Expressions 
           $where .= $where_array[$i]['base_expr']; 
          }        

          //append the LIKE '%$options['sSearch'])%' 
          $where .= ' AND '.$columns_array_val." LIKE '%".$options['sSearch']."%' OR "; 
         } 
        } 
       } 
      } 
      //remove the last OR 
      $where = substr_replace($where, "", -3);          
     } 
     else 
     { 
      //loop to build where 
      $i = 0; 
      $where_length = count($where_array); 
      for($i; $i < intval($where_length); $i++) 
      { 
       $where .= $where_array[$i]['base_expr']; 
      } 
     }    
    } 

    //print_r($where_length); 
    return $where; 
} 

回答

2

分解方法主要不是重用。这样做可以使代码更易于阅读,测试和维护。清除方法名称也可以替代内联注释。此方法执行两个高级别的事情:可以使用options和without选项构建where子句。对我来说,另一个暗示是,构建带有选项的where子句的逻辑看起来足够丰富,足以保证自己的方法。

private function build_where($query_array, $options) { 
    if(!empty($query_array['WHERE'])) { 
     $where_array = $query_array['WHERE']; 
     $columns_array = $this->build_columns_array($query_array); 
     if (empty($options['sSearch'])) { 
      return $this->build_where_with_options($where_array, $columns_array, $options); 
     } 
     else { 
      return $this->build_where_without_options($where_array, $columns_array); 
     } 
    } 
    else { 
     return ''; 
    } 
} 

现在,您可以快速扫描build_where()地看到,有三种可能的形式在where子句可以采取当与输入每个表单需要产生的结果一起。

这里有一些小的改进可以使整个代码:

  • count()返回一个整数,而不需要在for环路intval()电话。即使你把它们留在那里,最好在循环外部应用这个调用,因为它每次只产生一次,因为它每次产生相同的值。
  • if($searchable_column_val == true)相当于if($searchable_column_val),因为它们都将$searchable_column_val转换为布尔值,而后者在转换后的布尔值等于true时通过。
  • $where = substr_replace($where, "", -3)可以用$where = substr($where, 0, -3)代替,并且稍微清楚些。
  • 而不是循环查找特定键的数组,您可以通过简单地使用该键获取值来利用PHP的数组。

对于最后一个,这个代码

foreach($columns_array as $columns_array_key => $columns_array_val) 
{ 
    //if the $columns_array_key matches the $column_id 
    if($columns_array_key == $column_id) 
    { ... } 
} 

可以通过这个

$columns_array_val = $columns_array[$column_id]; 
... 
+0

真棒反馈的人!谢谢! – Peter 2011-02-26 23:01:07

1

个人喜好真的。一些程序员会将这些分成几个函数。就我个人而言,我认为你拥有它的方式很好。如果我看到一些我认为可以重用的东西,我会将它重构为一个可以包含的单独文件。

在我看来,有些程序员太快了,他们甚至没有什么可重用的东西,所以它们会让事情变得“难以估量”。

+0

更换谢谢你的答复。除了整个块以外,在这个块中没有任何可重用的东西。 – Peter 2011-02-26 22:13:36

5

Kent Beck或Martin Fowler的思想学派实际上会建议您将这些大型方法重构为许多小方法。在我看来,这并不容易理解,这将是重构的主要原因。

+0

感谢您的回复和解释。 – Peter 2011-02-26 22:12:33

相关问题