コード例 #1
0
ファイル: Filter.php プロジェクト: lagden/sf14libs
 /**
  *
  * @author Thiago Lagden
  */
 public static function query($filters, $fields, $table, $name = 'q')
 {
     $q = $table->getListQuery();
     $alias = $q->getRootAlias();
     if (isset($filters[$name]) && $filters[$name]) {
         if (method_exists($table, 'search')) {
             try {
                 // Removendo palavras pequenas
                 $fix = static::fix($filters[$name]);
                 // Try Searchable
                 $search = $table->search($fix);
                 $arr = array();
                 foreach ($search as $v) {
                     $arr[] = $v['id'];
                 }
                 if (count($arr) > 0) {
                     $q->orWhereIn("{$alias}.id", $arr);
                 }
             } catch (Exception $e) {
                 // No Searchable
             }
         }
         // Or Array
         $buildOr = array();
         foreach ($fields as $field) {
             $buildOr[] = "{$alias}.{$field} LIKE '%{$filters[$name]}%'";
         }
         // Add $buildOr in query
         if (count($buildOr)) {
             $q->andWhere(join(' OR ', $buildOr));
         }
     }
     // Added table fields
     if (!empty($filters)) {
         foreach ($filters as $k => $v) {
             if ($k != 'q' && $k != '_csrf_token' && Utils::stringIsNullOrEmpty($v) == false) {
                 $q->andWhere("{$alias}.{$k} = ?", $v);
             }
         }
     }
     // Sort
     $order = sfContext::getInstance()->getUser()->getAttribute(sfConfig::get('order_by'), sfConfig::get('order_by_default', 'id'));
     $direction = sfContext::getInstance()->getUser()->getAttribute(sfConfig::get('order_by_direction'), sfConfig::get('order_by_direction_default', 'DESC'));
     $q->orderBy("{$alias}.{$order} {$direction}");
     return $q;
 }