I use Zend Framework 2 with Doctrine 2. Here is my problem
我使用Zend Framework 2和Doctrine 2.这是我的问题
The following returns Array of Objects
以下命令返回对象数组
$results = $em->getRepository('MyProject\Domain\User')->find($id);
Returns:
返回:
array (size=4)
0 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
1 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
3 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
4 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
I want to convert it to Array of Arrays like so:
我想将它转换为数组数组,如下所示:
array (size=4)
0 =>
array (size=3)
['id'] => int 1
['firstName'] => string 'joe' (length=3)
['lastName'] => string 'smith' (length=5)
1 =>
array (size=3)
['id'] => int 1
['firstName'] => string 'joe' (length=3)
['lastName'] => string 'smith' (length=5)
2 =>
array (size=3)
['id'] => int 1
['firstName'] => string 'joe' (length=3)
['lastName'] => string 'smith' (length=5)
3 =>
array (size=3)
['id'] => int 1
['firstName'] => string 'joe' (length=3)
['lastName'] => string 'smith' (length=5)
I have tried the following:
我尝试过以下方法:
$resultsArray = new \Doctrine\Common\Collections\ArrayCollection($results);
$resultsArray->toArray();
$resultsArray = new \Zend\Stdlib\ArrayObject($results);
$resultsArray->getArrayCopy();
Both return this:
两者都返回:
array (size=4)
0 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
1 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
3 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
4 =>
object(My\Entity\User)[3]
private 'id' => int 1
private 'firstName' => string 'joe' (length=3)
private 'lastName' => string 'smith' (length=5)
How can I accomplish this? What is the recommended way of doing it?
我怎么能做到这一点?这样做的推荐方法是什么?
3 个解决方案
#1
1
In its simplest form, the following does what you want
在最简单的形式,以下做你想要的
$qb = $em->getRepository('My\Entity\User')->createQueryBuilder('User');
$result = $qb->getQuery()->getArrayResult();
Normally you'd use a custom repository and add your DQL queries as methods as describe here in the docs
通常,您使用自定义存储库并将DQL查询添加为文档中描述的方法
http://docs.doctrine-project.org/en/2.0.x/tutorials/getting-started-xml-edition.html#entity-repositories
#2
0
Traditionally, I write a custom repository class and call a getArrayResult()
inside of it
传统上,我编写一个自定义存储库类并在其中调用getArrayResult()
$dql = 'MYDQLHERE';
$query = $this->getEntityManager()->createQuery($dql);
$query->execute();
$result = $query->getArrayResult();
I believe your other option is custom hydration modes, although I don't have experience with them. http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes
我相信你的另一种选择是定制保湿模式,虽然我没有经验。 http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes
#3
0
Yet another way to code it, which I use:
还有另一种编码方式,我使用它:
//at the top of custom repository class
use Doctrine\Orm\Query;
//in the method
$dql = "SELECT u FROM MyProject\Domain\User u WHERE u.id = $id"
$query = $entityManager->createQuery($dql);
$users = $query->getResult(Query::HYDRATE_ARRAY);
//knowing the code you provided, this should give you an array of four arrays
#1
1
In its simplest form, the following does what you want
在最简单的形式,以下做你想要的
$qb = $em->getRepository('My\Entity\User')->createQueryBuilder('User');
$result = $qb->getQuery()->getArrayResult();
Normally you'd use a custom repository and add your DQL queries as methods as describe here in the docs
通常,您使用自定义存储库并将DQL查询添加为文档中描述的方法
http://docs.doctrine-project.org/en/2.0.x/tutorials/getting-started-xml-edition.html#entity-repositories
#2
0
Traditionally, I write a custom repository class and call a getArrayResult()
inside of it
传统上,我编写一个自定义存储库类并在其中调用getArrayResult()
$dql = 'MYDQLHERE';
$query = $this->getEntityManager()->createQuery($dql);
$query->execute();
$result = $query->getArrayResult();
I believe your other option is custom hydration modes, although I don't have experience with them. http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes
我相信你的另一种选择是定制保湿模式,虽然我没有经验。 http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes
#3
0
Yet another way to code it, which I use:
还有另一种编码方式,我使用它:
//at the top of custom repository class
use Doctrine\Orm\Query;
//in the method
$dql = "SELECT u FROM MyProject\Domain\User u WHERE u.id = $id"
$query = $entityManager->createQuery($dql);
$users = $query->getResult(Query::HYDRATE_ARRAY);
//knowing the code you provided, this should give you an array of four arrays