php - Doctrine2 LEFT JOIN AND 产生意想不到的结果

标签 php mysql sql symfony doctrine-orm

我有以下查询:

<?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/

相关文章:

php - Laravel Eloquent for 循环查询到单个准备好的查询

javascript - 通过ajax检索文档后添加日期选择器

python - 从远程数据库获取并存储到本地 Django

sql - Hadoop,处理带有分隔符但长度不同的文件

mysql - 如何通过一个查询删除两个链接表(多对多关系)中的数据?

php - HTML POST 上的 htmlspecialchar 到带有数组和单个输入的 PHP 脚本

php - 在 iFrame 上打开链接 - PHP 或 JQuery

基于字母代码的 PHP/SQL ID

mysql - 根据条件行从另一个表中查找结果

mysql - 如何获取 knex/mysql 中所有更新记录的列表