我在公司和客户之间有一对多的关系。我做了这个存储库并且它无法正常工作。
class CustomerRepository extends EntityRepository
{
public function searchCustomer($criteria)
{
$q = $this->createQueryBuilder('c')->join('TeamERPCustomerBundle:Company', 'o');
$q->orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.address like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.postal_address like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.city_town_village like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.e_mail like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.land_line like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.cell_phone like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.fax like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.cell_phone like '%s'", '%'.$criteria.'%'));
$q->Where(sprintf("o.company_name like '%s'", '%'.$criteria.'%'));
return $q->getQuery()->getResult();
}
}
如果任何字段匹配并且这不是我想要的,则查询返回整个表。我想要的是只返回与此$criteria
匹配的字段。我究竟做错了什么?
关心并感谢你。
答案 0 :(得分:0)
我想我找到了解决方案......仍在测试,但我认为现在很好:
class CustomerRepository extends EntityRepository
{
public function searchCustomer($criteria)
{
$q = $this->createQueryBuilder('c')
->join('TeamERPCustomerBundle:Company', 'o', 'WITH', 'c.company = o.id');
$q->orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.address like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.postal_address like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.city_town_village like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.e_mail like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.land_line like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.cell_phone like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.fax like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("c.cell_phone like '%s'", '%'.$criteria.'%'));
$q->orWhere(sprintf("o.company_name like '%s'", '%'.$criteria.'%'));
return $q->getQuery()->getResult();
}
}