将两个doctrine2查询合并为一个

时间:2013-10-11 05:05:58

标签: symfony orm doctrine-orm doctrine

我有以下查询:

  $query = $qb->select('DISTINCT s')
              ->from("MainBundle:Shop", 's')
              ->andWhere('SOUNDEX(s.fullname) = SOUNDEX(:shopName)');
  $parameter["shopName"] = $searchquery;
  $query->setParameters($parameter);

  $query = $qb->select('DISTINCT s')
              ->from("MainBundle:Shop", 's')
              ->andWhere($qb->expr()->like('s.fullname', $qb->expr()->literal('%' . $searchquery . '%')));

有可能两个将这两个查询合并为一个吗?

1 个答案:

答案 0 :(得分:1)

如果你的第二个查询有效,这应该有效:

$em = $this->getEntityManager();

$qb = $em->createQuery('
    SELECT DISTINCT s
    FROM MainBundle:Shop p
    WHERE Soundex(s.fullname) = :soundex(shopname) OR
        ($qb->expr()->like('s.fullname', $qb->expr()->literal('%' . $searchquery . '%')))
');
$parameter["shopName"] = $searchquery;
$query->setParameters($parameter);