2011-01-25 8 views
2

私はフロントエンドモジュール上の検索フィルタを作成しようとしています、 、datetimeフィールドを持っています。symfonyの - 日時フィールドの月ごとの検索テーブル、年2ヶ月であるフィールド、および年</p> <p>私は私のdbテーブル内の項目を持っていることでフィルタします

私はその後、2つのドロップダウン、などの日付時刻持っているすべてのアイテムから1月と2010を選択した場合、検索、作成できるようにしたいと思い

2010年1月24日10時50分に: 52

2010-01-25 10時50分52秒

は、私はsymfonyの1.4を使用していますが、PropelのORM

ありがとう

を一覧表示されます

答えて

2

2つの日付を作成し、その日付がそれらの間にあるかどうかを確認してください(たとえば、2010-01-01> = $ date & & 2010-01-31 < = $ date)。そうであれば、範囲内とすべての偶然が表示されます。

YEAR(日付)= $ yourDate、月(日付)= $ 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

+0

月と年で検索したいのですが? – terrid25

+0

私はその場合にYEAR()とMONTH()関数を使用しますが、それは絶対に必要なときだけです。日付の操作は非常に高価な操作です。 – guiman

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: public function executeExplore($ request) { //デフォルト:今月の最初の日 - 1年

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

今、私はDoctrineを使用していますので、モデル部分でPropeleseに少し翻訳しなければなりません。あなたはここでやっている「月ごとに分解された統計」を行っていないかもしれませんが、あなたが行くのを手伝ってください。がんばろう!

関連する問題