Symfony1 Symfony-按月、年搜索datetime字段的表格

Symfony1 Symfony-按月、年搜索datetime字段的表格,symfony1,propel,symfony-1.4,Symfony1,Propel,Symfony 1.4,我正在尝试在前端模块上创建一个搜索过滤器,它将按两个字段进行过滤,即月份和年份 我的db表中有一些项,它们有一个datetime字段 我希望能够创建一个搜索,如果我从2个下拉列表中选择“1月”和“2010年”,则所有包含日期时间的项目如下: 2010-01-24 10:50:52 2010-01-25 10:50:52 将被列为 我正在使用symfony 1.4和Propel ORM 谢谢为什么不试着创建两个日期,并检查该日期是否在它们之间,例如(2010-01-01>=$date&&2010-

我正在尝试在前端模块上创建一个搜索过滤器,它将按两个字段进行过滤,即月份和年份

我的db表中有一些项,它们有一个datetime字段

我希望能够创建一个搜索,如果我从2个下拉列表中选择“1月”和“2010年”,则所有包含日期时间的项目如下:

2010-01-24 10:50:52

2010-01-25 10:50:52

将被列为

我正在使用symfony 1.4和Propel ORM


谢谢

为什么不试着创建两个日期,并检查该日期是否在它们之间,例如(2010-01-01>=$date&&2010-01-31我在一个应用程序中得到了一些非常类似的东西。我正在使用for fancy date小部件

我的模式是“抱怨”,我的行动是“探索”

lib/form/ExploreForm.php:

apps/frontend/modules/complaint/templates/exploresucces.php:

实际查询位于模型lib/model/doctor/ComplaintTable.class.php中:

公共静态函数getMonthCounts($from,$to){
$connection=doctor_Manager::connection();

$query=我同意。有其他方法可以执行此操作,但这应该是最有效的。以用户提供的月份和年份为例。在00:00:00计算月的第一天,在23:59:59计算月的最后一天。这将允许非常有效的搜索。如月份、年份等数据库转换是资源消耗如果我想按月份和年份搜索呢?我会使用year()和month()函数,但只有在绝对必要时才使用。日期操作是一个非常昂贵的操作。这是在自定义查询中进行的,还是我可以将它们传递给criteria对象?它必须是自定义的,我不知道是否有与之相当的推进。但是使用自定义应该可以很好地工作
$criterias->addCriteria(TablePeer::DATE_ATTRIBUTE,"YEAR(". TablePeer::DATE_ATTRIBUTE .") = $year",Criteria::CUSTOM);
$criterias->addCriteria(TablePeer::DATE_ATTRIBUTE,"MONTH(". TablePeer::DATE_ATTRIBUTE .") = $month",Criteria::CUSTOM);
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()
                               )
                         );

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


  // ...

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