我想获得对象格式的 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();
结果集:
Array
(
[userid] => 4
[profileid] => 3
[username] => Test user
[email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="027667717677716770427b636a6d6d2c616d6f" rel="noreferrer noopener nofollow">[email protected]</a>
[isactive] => 1
[firstname] => MyFname
[lastname] => MyLname
[profileimage] => 5111ea998c9476c2231180050d5ad64dc3298fe0.jpeg
[address] => My Address
[zipcode] => 36102555
[biography] => This is my first symfon2.3 project.
)
最佳答案
可能是因为您没有完全加载对象,而是部分加载了它。这就是 ORM 自动转换它的原因。您可以使用 partial
关键字 to force它作为部分对象加载,但要小心。
关于symfony - 使用 QueryBuilder 的 Doctrine2 查询返回数组而不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772986/