我有以下查询:
<?php
$em = $this->getDoctrine()->getManager();
$data['ps'] = $em->createQuery('SELECT s,ps FROM AppBundle:Sucursales s LEFT JOIN AppBundle:ComprasProductosSucursales ps WITH s.id = ps.sucursal')->getResult();
但我得到如下图所示的结果:
![Doctrine2 结果集示例][1]
我期望 $data['ps'][1]
成为 $data['ps'][0]
的一部分,否则迭代数组将会变得非常奇怪。
那么,是否可以将连接表 AppBundle:ComprasProductosSucursales 的结果作为 AppBundle:Sucursales 数据的列来获取?
ORM:
AppBundle\Entity\Sucursales:
type: entity
table: sucursales
# Note: no relations defined, deleting the rest of the columns for clarity
AppBundle\Entity\ComprasProductosSucursales:
type: entity
table: compras_productos_sucursales
manyToOne:
producto:
targetEntity: ComprasProductos
sucursal:
targetEntity: Sucursales
cascade: { }
最佳答案
假设您有相关的实体,那么通常您的 DQL 查询将类似于:
$dql <<<EOT
SELECT s,ps FROM AppBundle:Sucursales s
LEFT JOIN s.ps ps
EOT;
Doctrine 负责处理连接上的 with 条件,并为您提供一组嵌套的结果。假设您的 Sucursales 有一个名为 ps 的集合属性。我想我还假设您正在创建 Doctrine 2 实体并让它们相关联。
更多信息:http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#joins
关于php - Doctrine2 LEFT JOIN AND 产生意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29061697/