php - Doctrine 如何在使用 addSelect 查询时在一行中获得结果

标签 php mysql symfony doctrine-orm

我的 SystemRepository 中有这个 Doctrine 查询:

        $qb = $this->createQueryBuilder('s')
        ->select('s')
        ->addSelect('item')
        ->from('Item', 'item')
        ->where('item.iId = s.id');

通过这个查询,我得到了这样的结果:

    Array
(
    [0] => System 1
    [1] => ItemObject related to SystemObject 1
    [2] => SystemObject 2
    [3] => ItemObject related to SystemObject 2
    [4] => SystemObject 3
    [5] => ItemObject related to SystemObject 3
)

但我想得到这样的结果:

    Array
(
    [0] => Array( 
            [0] => SystemObject 1
            [1] => ItemObject related to SystemObject 1
            ),
    [1] => Array( 
            [0] => SystemObject 2
            [1] => ItemObject related to SystemObject 2
            ),
    [2] => Array( 
            [0] => SystemObject 3
            [1] => ItemObject related to SystemObject 3
            )
)

这可能吗?

由于技术原因,我无法在系统和项目之间进行 Doctrine 映射。

最佳答案

我认为开箱即用是不可能的。但无论如何,在我看来,这两个结构看起来非常相似。您可以轻松应用基于 PHP 的分组:

$grouped = [];
for ( $i = 0; $i < count($data); $i+=2){
    $grouped[] = [
        $data[$i],
        $data[$i+1]
    ];
}

希望这有助于...

关于php - Doctrine 如何在使用 addSelect 查询时在一行中获得结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150532/

相关文章:

php - Symfony i18n 路由 : partial language support

Symfony2 createFormBuilder 字段名称前缀不带实体

Symfony2,错误 "The uploaded file was too large."没有文件字段

php - 可更改 Woocommerce 购物车商品价格的产品自定义复选框选项

php - Woocommerce 在添加新产品之前删除购物车

mysql - Laravel Eloquent 组按最新记录

mysql - 需要从特定选定记录中过滤重复记录

mysql - 如何获取mysql中最后一条之前的最后一条记录?

php - 使用 Symfony Messenger 异步发送电子邮件时如何翻译电子邮件?

php - 删除行后如何更新 yii CGridview 摘要