2012-10-15 51 views
4

我试图构建一个响应于来自用户的自定义搜索的动态查询。我有一个问题,当我构建查询时,由于SELECT LIKE列比较不能使用NULL值,所以我没有结果。考虑到这个查询是在建立的,我怎样才能解决这个问题?因此,用户可以给值或不为搜索条件...Symfony2 - 查询生成器LIKE空值

这是我的代码:

$qb->add('select', 'f') 
    ->add('from', 'Bundle:Object f') 
    ->add('where', $qb->expr()->andx(
      $qb->expr()->like('f.c1',':c1'), 
      $qb->expr()->like('f.c2',':c2'), 
      $qb->expr()->like('f.c3',':c3'))) 
    ->add('orderBy', 'f.nnumCatalogo ASC'); 
if ($data->getField1() != null) { 
    $isField1 = true; 
} 
if ($data->getField2() != null) { 
    $isField2 = true; 
} 
if ($data->getField3() != null) { 
    $isField3 = true; 
} 
if ($isField1) { 
    $qb->setParameter('c1', $data->getField1()); 
} else { 
    $qb->setParameter('c1', '%'); 
} 
if ($isField2) { 
    $qb->setParameter('c2', $data->getField2()); 
} else { 
    $qb->setParameter('c2', '%'); 
} 
if ($isField3) { 
    $qb->setParameter('c3', $data->getField3()); 
} else { 
    $qb->setParameter('c3', '%'); 
} 

有了这个代码,我有没有结果监守在某些列不与LIKE“%选择NULL值'(mysql)。

问候,

哈维尔

回答

0

试试这个:

$qb->add('select', 'f')->add('from', 'Bundle:Object f'); 
if ($data->getField1() != null) { 
    $qb->andWhere('f.c1 like :c1') 
    ->setParameter('c1', $data->getField1()); 
} 
if ($data->getField2() != null) { 
    $qb->andWhere('f.c2 like :c2') 
    ->setParameter('c2', $data->getField2()); 
} 
if ($data->getField3() != null) { 
    $qb->andWhere('f.c3 like :c3') 
    ->setParameter('c3', $data->getField3()); 
} 
$qb->add('orderBy', 'f.nnumCatalogo ASC'); 
相关问题