探查器显示此查询:
SELECT
count(DISTINCT c0_.id) AS sclr0
FROM
customers c0_
LEFT JOIN customers_addresses c1_ ON c0_.id = c1_.customer_id
LEFT JOIN customers_phones c2_ ON c0_.id = c2_.customer_id
WHERE
(
c0_.is_lead = ?
AND c0_.firstname LIKE ?
)
OR c0_.lastname LIKE ?
OR c0_.email LIKE ?
OR c0_.company LIKE ?
OR c1_.street_address1 LIKE ?
OR c1_.street_address2 LIKE ?
OR c1_.city LIKE ?
OR c1_.state = ?
OR c1_.zipcode = ?
OR c2_.phone LIKE ?
显然不是我想要的,我希望它是这样的:
SELECT
count(DISTINCT c0_.id) AS sclr0
FROM
customers c0_
LEFT JOIN customers_addresses c1_ ON c0_.id = c1_.customer_id
LEFT JOIN customers_phones c2_ ON c0_.id = c2_.customer_id
WHERE
(
c0_.is_lead = ?
)
AND
(c0_.firstname LIKE ?
OR c0_.lastname LIKE ?
OR c0_.email LIKE ?
OR c0_.company LIKE ?
OR c1_.street_address1 LIKE ?
OR c1_.street_address2 LIKE ?
OR c1_.city LIKE ?
OR c1_.state = ?
OR c1_.zipcode = ?
OR c2_.phone LIKE ?
)
我的查询构建器如下所示:
public function search($keywords = null, $lead=0){
if ($keywords === null) return $this->findAllOrderedByName($lead);
$qb = $this->createQueryBuilder ('c')
->addSelect('a')
->addSelect('p')
->leftJoin('c.addresses', 'a')
->leftJoin('c.phones', 'p');
$qb->where('c.isLead = :lead');
$qb->andWhere('c.firstname like :firstname');
$qb->orWhere('c.lastname like :lastname');
$qb->orWhere('c.email like :email');
$qb->orWhere('c.company like :company');
$qb->orWhere('a.streetAddress1 like :streetAddress1');
$qb->orWhere('a.streetAddress2 like :streetAddress2');
$qb->orWhere('a.city like :city');
$qb->orWhere('a.state = :state');
$qb->orWhere('a.zipcode = :zipcode');
$qb->orWhere('p.phone like :phone');
$qb->setParameter('lead', $lead);
$qb->setParameter('firstname', '%' . $keywords . '%');
$qb->setParameter('lastname', '%' . $keywords . '%');
$qb->setParameter('email', '%' . $keywords . '%');
$qb->setParameter('company', '%' . $keywords . '%');
$qb->setParameter('streetAddress1', '%' . $keywords . '%');
$qb->setParameter('streetAddress2', '%' . $keywords . '%');
$qb->setParameter('city', '%' . $keywords . '%');
$qb->setParameter('state', $keywords);
$qb->setParameter('zipcode', $keywords);
$qb->setParameter('phone', '%' . $keywords . '%');
$qb->orderBy('c.lastname', 'ASC');
return $qb;
}
我目前拥有查询构建器的方式返回结果,即使搜索关键字由于连接而不匹配任何记录,我不知道如何修改查询构建器方法,任何想法?
答案 0 :(得分:0)
您是否尝试过使用addAnd(<condition>)
代替andWhere()
?
答案 1 :(得分:0)
你可以把它写成
$qb->andWhere('(c.firstname like :firstname
OR c.lastname like :lastname
OR c.email like :email
OR c.company like :company
OR a.streetAddress1 like :streetAddress1
OR a.streetAddress2 like :streetAddress2
OR a.city like :city
OR a.state = :state
OR a.zipcode = :zipcode
OR p.phone like :phone)');
答案 2 :(得分:0)
我认为您可以使用orX
,然后将其重写为..
public function search($keywords = null, $lead=0){
if ($keywords === null) return $this->findAllOrderedByName($lead);
$qb = $this->createQueryBuilder ('c')
->addSelect('a')
->addSelect('p')
->leftJoin('c.addresses', 'a')
->leftJoin('c.phones', 'p');
$qb->where(
$qb->expr()->eq('c.isLead', ':lead')
)->andWhere(
$qb->expr()
->orX($qb->expr()->like('c.firstname', ':keywords'))
->add($qb->expr()->like('c.lastname', ':keywords'))
->add($qb->expr()->like('c.email', ':keywords'))
->add($qb->expr()->like('c.company', ':keywords'))
->add($qb->expr()->like('a.streetAddress1', ':keywords'))
->add($qb->expr()->like('a.streetAddress2', ':keywords'))
->add($qb->expr()->like('a.city', ':keywords'))
->add($qb->expr()->eq('a.state', ':keywords'))
->add($qb->expr()->eq('a.zipcode', ':keywords'))
->add($qb->expr()->like('p.phone', 'keywords'))
);
$qb->setParameter('lead', $lead);
$qb->setParameter('keywords', '%' . $keywords . '%');
$qb->orderBy('c.lastname', 'ASC');
return $qb;
}