php - 具有连接的 Doctrine 查询构建器返回实体数组而不是分组结果

标签 php mysql doctrine-orm doctrine

我对 Doctrine 有一些疑问。我正在尝试执行 leftJoin,遵循以下问题:How to do left join in Doctrine?

这是我的查询:

$qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('a', 'u')
        ->from('Application\Entity\Event', 'a')
        ->leftJoin(
            'Application\Entity\Venue',
            'u',
            \Doctrine\ORM\Query\Expr\Join::WITH,
            'a.venue_id = u.id'
        )
        ->orderBy('a.event_datetime', 'DESC');

    var_dump($qb->getQuery()->getResult());

结果如下:

array (size=4)
0 => 
object(Application\Entity\Event)[483]
  protected 'id' => int 8
  protected 'artist_id' => int 4
  protected 'venue_id' => int 1246
  protected 'name' => string 'Vlasta Redl' (length=11)
1 => 
object(Application\Entity\Venue)[477]
  protected 'id' => int 1246
  protected 'name' => string 'Malostranská beseda' (length=20)
  ...
2 => 
object(Application\Entity\Event)[468]
  protected 'id' => int 7
  protected 'artist_id' => int 3
  protected 'venue_id' => int 761
  protected 'name' => string 'Positive Mind' (length=13)

3 => 
object(Application\Entity\Venue)[485]
  protected 'id' => int 761
  protected 'name' => string 'Divadlo pod lampou' (length=18)
  ....

这是一个不同对象的数组。事件和 field 。

但我期待这样的结果:

array(
    array(
        0 => Event
        1 => Venue,
    ),
    array(
        0 => Event
        1 => Venue,
    ),
    // ...
)

我做错了什么?

最佳答案

我试过了,但我做不到。

我这样做了:

foreach($result as $object){
            if(get_class($object)=='Panel\PollBundle\Entity\PollProfile'){
                $pollProfile[] = $object;
            }
            else if (get_class($object)=='Panel\PollBundle\Entity\Poll') {
                $poll[] = $object;
            }
        }

关于php - 具有连接的 Doctrine 查询构建器返回实体数组而不是分组结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27964841/

相关文章:

mysql - Symfony2 存储库和自定义连接

php - 如何将表字段映射到学说实体中不同名称的变量?

php - 多次更新登录用户表中的字段

php - magento 1.9.1.0 local.xml 不工作

c++ - Qmysql 驱动未加载但可用

php - 如何通过 Ajax 获取 php 返回值?

php - 只显示返回的 MySQL 查询的一部分?

php - 错误的 json 数组 Python post 请求到 PHP

mysql - 创建一个将 "chained"id 转换为 "grouped"id 的查询

doctrine-orm - Symfony 4 Doctrine 2 自定义 NamingStrategy 如何