不需要参数时在 Doctrine 中使用 join 语句
Using a join statement in Doctrine when no parameters are required
我正在尝试在我的搜索功能中向我的 DQL 添加一个连接语句,因为用户可以搜索的字段之一实际上与另一个 table 链接,特别是仅引用的客户名称在根 table 作为 id.
我已经在 Doctrine 站点上看到了连接语法应该如何工作(仅在下面的示例):
$qb->join('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
但我不想匹配特定值,我只想像在 mySQL 中那样加入它,如下所示:
...JOIN table2 ON table2.id = table1.existingCustomer
这是我当前的搜索 DQL,其中 m
代表一个名为 Message 的 table,c
代表一个名为 Customer 的 table:
$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
的引用,它是客户 table 的别名,因为消息 table 仅使用一个 id 来交叉引用它.
我需要知道的是,有没有办法通过将消息 table 中的 existingCustomer
字段链接到 id
字段来加入客户 table客户 table 而不必匹配特定值才能使这项工作?
你试过这个吗:
us \Doctrine\ORM\Query\Expr\Join;
// ...
->leftJoin('YourBundle:Table2', 'table2', Join::WITH, 'table2.id = table1.existingCustomer')
我正在尝试在我的搜索功能中向我的 DQL 添加一个连接语句,因为用户可以搜索的字段之一实际上与另一个 table 链接,特别是仅引用的客户名称在根 table 作为 id.
我已经在 Doctrine 站点上看到了连接语法应该如何工作(仅在下面的示例):
$qb->join('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
但我不想匹配特定值,我只想像在 mySQL 中那样加入它,如下所示:
...JOIN table2 ON table2.id = table1.existingCustomer
这是我当前的搜索 DQL,其中 m
代表一个名为 Message 的 table,c
代表一个名为 Customer 的 table:
$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
的引用,它是客户 table 的别名,因为消息 table 仅使用一个 id 来交叉引用它.
我需要知道的是,有没有办法通过将消息 table 中的 existingCustomer
字段链接到 id
字段来加入客户 table客户 table 而不必匹配特定值才能使这项工作?
你试过这个吗:
us \Doctrine\ORM\Query\Expr\Join;
// ...
->leftJoin('YourBundle:Table2', 'table2', Join::WITH, 'table2.id = table1.existingCustomer')