如何在symfony2中使用学说的findOneBy方法返回数组而不是对象? [英] How to return an array not an object with doctrine findOneBy method in symfony2?
本文介绍了如何在symfony2中使用学说的findOneBy方法返回数组而不是对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到一种情况,我想从symfony2中的学说中使用findOneBy($ id)方法查询数据库。
I have a situation in which I want to query the database with findOneBy($id) method from doctrine in symfony2.
$namePosting = $this->getDoctrine()->getRepository('MyBundle:Users')->findOneById($userPosting);
结果它是具有受保护属性的对象。我想直接将其返回一个数组。如何才能做到这一点 ?
The result it's an object with protected properties. I want to return it directly an array. How can this be done ?
推荐答案
findOneBy(array())
始终返回null或对象
findOneBy(array())
will always return null or object.
,但是您可以改用 findById($ userPosting)
或 findBy(array( 'id'=> $ userPosting))
,它将返回一个数组,例如:
But you can use instead findById($userPosting)
or findBy(array('id' => $userPosting))
and it will return an array, e.g.:
$this->getDoctrine()->getRepository('MyBundle:Users')->findById($userPosting))
已编辑
或者您可以在 UserRepository
类中添加方法:
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
class UserRepository extends EntityRepository
{
public function getUser($userPosting)
{
$qb = $this->createQueryBuilder('u')
->select('u')
->where('u =:userPosting')->setParameter('userPosting', $userPosting)
->getQuery()
->getResult(Query::HYDRATE_ARRAY);
return $qb;
}
}
这篇关于如何在symfony2中使用学说的findOneBy方法返回数组而不是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文