简体   繁体   中英

search with Doctrine if blank field

i have written a DQL query for my multiple search,working fine if i specify the all fields but if i keep one of the field blank it won't show me the result

          $query = $em->createQuery
          ('SELECT d FROM EntityBundle:Doctor d WHERE d.name =:name AND d.degree =:degree AND d.area = :area_id AND d.sex = :sex AND
           (

                 ( YEAR(d.dob)  BETWEEN :d2 AND :d1 ) OR
                 ( YEAR(d.dob)  BETWEEN :e2 AND :e1 ) OR
                 ( YEAR(d.dob)  BETWEEN :f2 AND :f1 )

            )'          )

           ->setParameter('name', $name)
           ->setParameter('degree', $degree)
            ->setParameter('area_id', $area)
            ->setParameter('sex', $sex)
            ->setParameter('d1', $this->p1)
            ->setParameter('d2', $this->p2)
            ->setParameter('e1', $this->q1)
            ->setParameter('e2', $this->q2)
            ->setParameter('f1', $this->r1)
            ->setParameter('f2', $this->r2)

          ->getResult();

i have tried setParameter('name', (is_null($name)? "IS NULL" : $name)) also but still there is no luck..

You can use QueryBuilder instead of DQL

$qb = $qb->select('d');
if($name !== null) {
     $qb = $qb->where('d.name = :name')->setParameter(...);
}
...
$qb->getQuery()->getResult();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM