ResultSetMapping addScalarResult关联数组

时间:2016-04-10 03:57:41

标签: php symfony doctrine-orm doctrine-native-query

在我使用Symfony2的项目中,我正在使用Doctrine createNativeQuery,我想获得一个关联数组。

这是我的代码

$rsm = new ResultSetMapping;
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('phone_one', 'phoneOne');
$rsm->addScalarResult('phone_two', 'phoneTwo');

我有这个结果:

array:2 [
    0 => array:4 [
        "id" => "975"
        "name" => "one name"
        "phoneOne" => "122345556"
        "phoneTwo" => "345566789"
    ]
  1 => array:4 [
    0 => array:4 [
        "id" => "976"
        "name" => "two name"
        "phoneOne" => "122345556"
        "phoneTwo" => "345566789"
    ]
]

这个结果是否可行?

array:2 [
    0 => array:4 [
        "id" => "975"
        "name" => "one name"
        "phones" => [
            "phoneOne" => "122345556"
            "phoneTwo" => "345566789"
        ]
    ]
  1 => array:4 [
    0 => array:4 [
        "id" => "976"
        "name" => "two name"
        "phones" => [
            "phoneOne" => "122345556"
            "phoneTwo" => "345566789"
        ]
    ]
]

非常感谢

1 个答案:

答案 0 :(得分:1)

如果这是您想要的结果,为什么不为用户创建OneToMany手机?我强烈建议你这样做。