コード例 #1
0
ファイル: Propel.php プロジェクト: varvanin/currycms
 /**
  * Get search criteria with filters and sorting applied.
  *
  * @return ModelCriteria
  */
 public function getCriteria()
 {
     // build criteria
     $q = clone $this->query;
     // search field
     if ($_POST['query'] && $_POST['qtype']) {
         $qtype = $_POST['qtype'];
         $query = $_POST['query'];
         if ($this->tableMap->hasColumn($qtype)) {
             $field = $this->modelClass . "." . $this->tableMap->getColumn($qtype)->getPhpName();
         } elseif (Curry_Propel::hasI18nColumn($qtype, $this->tableMap)) {
             $i18nBehavior = Curry_Propel::getBehavior('i18n', $this->tableMap);
             $q->joinI18n(isset($_GET['locale']) ? $_GET['locale'] : $i18nBehavior['default_locale']);
             $field = "{$this->modelClass}I18n" . '.' . Curry_Propel::getI18nColumn($qtype, $this->tableMap)->getPhpName();
         } else {
             $field = $this->modelClass . "." . $qtype;
         }
         $searchMethod = isset($this->searchMethod[$qtype]) ? $this->searchMethod[$qtype] : self::SEARCH_LIKE;
         switch ($searchMethod) {
             case self::SEARCH_EQUAL:
                 $q->where("{$field} = ?", $query);
                 break;
             case self::SEARCH_LIKE:
                 $q->where("{$field} " . Criteria::LIKE . " ?", "%{$query}%");
                 break;
             case self::SEARCH_TERMS_ANY:
             case self::SEARCH_TERMS_ALL:
                 $terms = preg_split('/\\s+/', trim($query));
                 $conditions = array();
                 foreach ($terms as $i => $term) {
                     $name = 'c' . $i;
                     $q->condition($name, "{$field} " . Criteria::LIKE . " ?", "%{$term}%");
                     $conditions[] = $name;
                 }
                 $q->combine($conditions, $searchMethod == self::SEARCH_TERMS_ALL ? Criteria::LOGICAL_AND : Criteria::LOGICAL_OR);
                 break;
             default:
         }
     }
     // sort on field
     if ($_POST['sortname'] && $_POST['sortorder']) {
         $q->clearOrderByColumns();
         try {
             if ($this->tableMap->hasColumn($_POST['sortname'])) {
                 $field = $this->tableMap->getColumn($_POST['sortname'])->getPhpName();
             } else {
                 // Is raw PhpName in case of virtualColumns
                 $field = $_POST['sortname'];
             }
             if ($_POST['sortorder'] == 'asc') {
                 $q->orderBy($field, CRITERIA::ASC);
             }
             if ($_POST['sortorder'] == 'desc') {
                 $q->orderBy($field, CRITERIA::DESC);
             }
         } catch (Exception $e) {
             trace($e);
         }
     }
     return $q;
 }