Doctrine 2 Dynamic Query

I’m trying to make a dynamic search query with doctrine. And it’s not working

$em = $this->getDoctrine()->getEntityManager();
$city = 'new york';
$minprice = 500;
$maxprice = 1000;

$testquery = "SELECT p FROM AcmeTestBundle:Car p WHERE p.city = :city";
$testparam = array('city' => $city,);

if ($minprice != '') {
    if ($maxprice != '') {
        $testquery .= " and p.price BETWEEN :minprice AND :maxprice";
        $testparam .= array(
            'minprice' => $minprice,
            'maxprice' => $maxprice,
            );
    }
}

$testquery .= " ORDER BY p.price ASC";

$query = $em->createQuery($testquery)->setParameters($testparam);

$result = $query->getResult();

Is there a better way to do this?

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories