Search code examples
phpsymfony1propel

Multiple criteria for single field in symfony?


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?


Solution

  • 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);