在不需要参数时在Doctrine中使用连接语句
问题描述:
我试图在我的搜索功能中向我的DQL添加连接语句,因为用户可以搜索的一个字段实际上与另一个表,特别是客户仅在根表中作为id引用的名称。在不需要参数时在Doctrine中使用连接语句
我已经看到了如何联接语法应在原则网站(例如只有跌破)工作:
$qb->join('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
,但我并不想匹配特定的值,我只是想加入的方式我将在MySQL作为如下:
...JOIN table2 ON table2.id = table1.existingCustomer
这里是我当前的搜索DQL,其中m
表示表称为Message和c
表示表名为Customer:
$qb = $this->createQueryBuilder('m');
$qb->select('m');
$qb->where('c.firstName LIKE :s');
$qb->orWhere('c.surname LIKE :s');
$qb->orWhere('m.postcode LIKE :s');
$qb->orWhere('m.town LIKE :s');
$qb->orWhere('m.phone1 LIKE :s');
$qb->andWhere('m.archived = :archived');
$qb->orderBy('m.messageDate', 'DESC')
->setParameter('s', '%'.$s.'%')
->setParameter('archived', 0);
return $qb->getQuery()->getResult();
正如你所看到的,有一个引用c
这意味着作为Customer表的别名,因为Message表只使用一个id来交叉引用this。
我需要知道的是,有没有办法通过将消息表中的existingCustomer
字段链接到客户表的id
字段,而无需匹配特定的值以使其工作?
答
你试试这个:
us \Doctrine\ORM\Query\Expr\Join;
// ...
->leftJoin('YourBundle:Table2', 'table2', Join::WITH, 'table2.id = table1.existingCustomer')