使用QueryBuilder进行Doctrine2查询返回数组而不是对象

时间:2014-11-06 06:22:25

标签: symfony doctrine-orm symfony-2.3

我想以对象格式获取doctrin2查询结果,但它以数组格式提供给我。 我试过但我不能成功。

这是演示查询,但是如果我想在另一个查询中应用类似类型的连接,那么也需要对象格式的结果,但是在所有这些中都需要。

你能解决吗?

return $this->createQueryBuilder("u")
->select("u.id as userid, up.id as profileid, u.username, u.email, u.isactive, up.firstname, up.lastname, up.profileimage, up.address, up.zipcode, up.biography")
->innerjoin("u.userprofile", "up")
->where("u.id = :userid")
->setParameter(":userid", $userid)
->getQuery()
->getResult();

ResultSet:

Array
(
    [userid] => 4
    [profileid] => 3
    [username] => Test user
    [email] => testuser@yahoo.com
    [isactive] => 1
    [firstname] => MyFname
    [lastname] => MyLname
    [profileimage] => 5111ea998c9476c2231180050d5ad64dc3298fe0.jpeg
    [address] => My Address
    [zipcode] => 36102555
    [biography] => This is my first symfon2.3 project.
)

1 个答案:

答案 0 :(得分:3)

可能是因为您没有完全加载对象,因为您正在部分加载它。这就是ORM自动转换它的原因。您可以使用partial关键字to force作为部分对象加载,但要小心。