symfony中单个字段的多个标准?

时间:2009-04-10 18:57:56

标签: php symfony1 propel

我正在尝试为symfony中的单个字段定义多个条件,但每次我定义第二个条件时,它都会覆盖前一个条件。以下是我的代码示例:

  # build query to fetch search results
  $c = new Criteria();

  # set minimum price
  if($request->getPostParameter('price_from') > 0)
  {
    $c->add(HomeModelPeer::PRICE,
            $request->getPostParameter('price_from'),
            Criteria::GREATER_EQUAL);
  }

  # set maximum price
  if($request->getPostParameter('price_to') > 0)
  {
    $c->add(HomeModelPeer::PRICE,
            $request->getPostParameter('price_to'),
            Criteria::LESS_EQUAL);
  }

有没有办法为每个字段定义多个?

1 个答案:

答案 0 :(得分:3)

对于同时具有这两种限制的情况,您需要执行以下操作:

$c = new Criteria();
$c2 = $c->getNewCriterion(HomeModelPeer::PRICE,
    $request->getPostParameter('price_from'),
    Criteria::GREATER_EQUAL);
$c2->addAnd($c->getNewCriterion(HomeModelPeer::PRICE,
    $request->getPostParameter('price_to'),
    Criteria::LESS_EQUAL)
    );
$c->add($c2);