views:

327

answers:

1

I'm trying to define multiple criteria for a single field in symfony, but every time I define a second criterion, it overwrites the previous one. Here is an example of my code:

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

Is there a way to define more than one per field?

+1  A: 

For the case where you have both limits, you need to do something like this:

$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);
MarkusQ
Thank you. This is exactly what I was looking for.
Jon Mooring