简体   繁体   English

Symfony 2 Doctrine DQL联接查询生成器

[英]Symfony 2 Doctrine DQL join query builder

I have a one to many relationship between company and customer. 我在公司和客户之间有一对多的关系。 I did this repository and it's not working properly. 我做了这个存储库,它不能正常工作。

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();
    }
}

The query is returning the whole tables if any of the fields match and that is not what I want. 如果有任何字段匹配,查询将返回整个表,这不是我想要的。 What I want is to return only the fields that match this $criteria . 我想要的是仅返回与此$criteria匹配的字段。 What am I doing wrong? 我究竟做错了什么? Regards and thank you. 谢谢,谢谢。

I think i found the solution... still testing but I think it's fine now: 我认为我找到了解决方案...仍在测试中,但我现在认为还可以:

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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