2011-01-25 59 views
2

我想創建一個前端模塊上的搜索過濾器,將通過2場,這是幾個月甚至幾年Symfony的 - 每月搜索表,今年的日期時間字段

我有我的數據庫表中的項目篩選,有一個日期時間字段。

我希望能夠創建一個搜索,在那裏如果我從2個下拉菜單,然後有樣日期時間的所有項目選擇2010年1:

2010-01-24 10:50: 52

2010-01-25 10時50分52秒

將上市

我使用的symfony 1.4,推進ORM

謝謝

回答

2

你爲什麼不嘗試創建2日期和檢查,如果該日期是他們之間,例如(2010-01-01> = $日期& & 2010-01-31 < = $日期)。如果是這樣,那麼你的範圍和所有巧合將出現。

如果你絕對要檢查月份和年份,我建議使用YEAR(日期)= $ yourDate和Month(日期)= $ yourMonth等函數,這個函數應該與CUSTOM標準一起使用,可能如下所示:

$criterias->addCriteria(TablePeer::DATE_ATTRIBUTE,"YEAR(". TablePeer::DATE_ATTRIBUTE .") = $year",Criteria::CUSTOM); 
$criterias->addCriteria(TablePeer::DATE_ATTRIBUTE,"MONTH(". TablePeer::DATE_ATTRIBUTE .") = $month",Criteria::CUSTOM); 

這裏是MysqlDateFunctions

+0

我同意。還有其他方法可以執行此操作,但這應該是最有效的。請提供用戶的月份和年份。計算每月00:00:00的月份的第一天以及每個月的最後一天23:59:59。這將允許非常有效的搜索。像MONTH,YEAR等數據庫轉換是耗費資源並且經常繞過索引。 – Jestep 2011-01-25 23:22:36

0

我有在應用程序非常類似的鏈接。我使用的是sfFormExtraPlugin花式日期控件。

我的模式是「投訴」和我的動作是「探索」。

LIB /形式/ ExploreForm.php

class ExploreForm extends BaseForm 
{ 
    public function configure() 
    { 

    $this->setWidgets 
     (array(
      'explore_range' => new sfWidgetFormDateRange 
      (array(
        'from_date' => new sfWidgetFormJQueryDate(), 
        'to_date' => new sfWidgetFormJQueryDate(), 
        'label' => 'Date of Service ranging ', 
        ) 
      ) 
      ) 
     ); 

    $this->setValidators(array(
           'explore_range'  => new sfValidatorDateRange 
           (array(
             'required' => true, 
             'from_date' => new sfValidatorDate(array('required' => false)), 
             'to_date' => new sfValidatorDate(array('required' => false)) 
            )), 
           'from' => new sfValidatorPass(), 
           'to' => new sfValidatorPass() 
           ) 
         ); 

    } 
} 

應用/前端/模塊/投訴/模板/ exploreSuccess.php

<form action="<?php echo url_for('complaint/explore') ?>" method="GET"> 
    <input type="submit" value="Change date range" style="float:right" /> 
    <ul> 
<?php echo $form->renderUsing('list') ?> 
    </ul> 
</form> 

應用/前端/模塊/投訴/動作/的actions.class.php: 公共函數executeExplore($請求) { //默認:這個月的第一天 - 周

$this->form = new ExploreForm(array(
       'explore_range' => array (
          'from' => $a_year_ago, 
          'to' => $last_of_last_month 
          ) 
       )); 

    if ($request->hasParameter('explore_range')) { 
$this->form->bind(array('explore_range' => $request->getParameter('explore_range'))); 
$this->logMessage("bound", "debug"); 
if ($this->form->isValid()) { 
    $this->form_values = $this->form->getValues(); # cleaned 
    $this->logMessage("validation WIN", "debug"); 
} 
else { 
    $this->logMessage("validation FAIL", "debug"); 
    $this->form_values = $this->form->getDefaults(); 
} 

    } 
    else { 
$this->logMessage("no explore_range param", "debug"); 
$this->form_values = $this->form->getDefaults(); 
    } 

    $this->from = $this->form_values['explore_range']['from']; 
    $this->to = $this->form_values['explore_range']['to']; 


    /* complaints per month */ 
    $this->complaints_by_month = ComplaintTable::getMonthCounts($this->from, $this->to); 


    // ... 

} 

和實際查詢是在模型中,的lib /模型/教義/ ComplaintTable.class.php

public static function getMonthCounts($from, $to) { 

    $connection = Doctrine_Manager::connection(); 
    $query = <<<ENDSQL 
    SELECT year(`date`) as y, month(`date`) as m, count(*) as c 
    FROM `complaints`.`complaint` 
    WHERE `date` BETWEEN ? AND ? 
    GROUP BY year(`date`), month(`date`) 
ENDSQL; 

    $statement = $connection->execute($query, array($from, $to)); 

    $result = array(); 
    while ($row = $statement->fetch()) { 
    $result[ sprintf("%04d-%02d",$row[0], $row[1]) ] = $row[2]; 
    } 

    return self::addZeroRows($result, $from, $to); 
} 

public static function addZeroRows($set, $from, $to) { 
    /* insert zero counts for months with no count */ 
    $from_fields = date_parse($from); 
    $to_fields = date_parse($to); 
    $start_y = $from_fields['year']; 
    $end_y = $to_fields['year']; 
    $start_m = $from_fields['month']; 
    $end_m = $to_fields['month']; 

    $i = 0; 
    for ($y = $start_y; $y <= $end_y; $y++) { 
    for ( $m = ($y == $start_y ? $start_m : 1) ; 
      ($y == $end_y && $m <= $end_m) || ($y < $end_y && $m <= 12); 
      $m++ 
      ) { 
     $y_m = sprintf("%04d-%02d",$y,$m); 
     if (!isset($set[$y_m])) { 
     $set[$y_m] = 0; 
     } 
     if ($i++ > 100) { // don't infinitely loop... you did it wrong 
     return $set; 
     } 
    } 
    } 


    ksort($set); 
    return $set; 
} 

現在,我使用的原則,所以你必須做一些翻譯成Propelese模型中的一部分,你可能不會做「統計按月細分」的事情我在這裏做,但應該幫助你走了。祝你好運!

相關問題