php Symfony2:如何将“Doctrine->getRepository->find”从实体转换为数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24147056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2: How to convert 'Doctrine->getRepository->find' from Entity to Array?
提问by Vladimir Hidalgo
I want to return an \Symfony\Component\HttpFoundation\JsonResponse with the record information, but I need to pass it as array.
我想用记录信息返回一个 \Symfony\Component\HttpFoundation\JsonResponse,但我需要将它作为数组传递。
Currently I do:
目前我这样做:
$repository = $this->getDoctrine()->getRepository('XxxYyyZzzBundle:Products');
$product = $repositorio->findOneByBarCode($value);
But now $product is an Entity containing all what I want, but as Objects. How could I convert them to arrays?
但现在 $product 是一个包含我想要的所有东西的实体,但作为对象。我如何将它们转换为数组?
I read somewhere that I need to use "Doctrine\ORM\Query::HYDRATE_ARRAY" but seems 'findOneBy' magic filter does not accept such parameter.
我在某处读到我需要使用“Doctrine\ORM\Query::HYDRATE_ARRAY”但似乎“findOneBy”魔术过滤器不接受这样的参数。
*
* @return object|null The entity instance or NULL if the entity can not be found.
*/
public function findOneBy(array $criteria, array $orderBy = null)
Well thanks to dbrumann I got it working, just want to add it the full working example.
好吧,多亏了 dbrumann,我才让它工作,只是想将它添加到完整的工作示例中。
Also seems that ->from() requires 2 parameters.
似乎 ->from() 也需要 2 个参数。
$em = $this->getDoctrine()->getManager();
$query = $em->createQueryBuilder()
->select('p')
->from('XxxYyyZzzBundle:Products','p')
->where('p.BarCode = :barcode')
->setParameter('barcode', $value)
->getQuery()
;
$data = $query->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
回答by dbrumann
When you want to change the hydration-mode I recommend using QueryBuilder
:
当您想更改水合模式时,我建议使用QueryBuilder
:
$query = $em->createQueryBuilder()
->select('p')
->from('Products', 'p')
->where('p.BarCode = :barcode')
->setParameter('barcode', $valur)
->getQuery()
;
$data = $query->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
But what would probably be better, is adding a toArray()
- or toJson()
-method to your model/entity. This way you don't need additional code for retrieving your entities and you can change your model's data before passing it as JSON, e.g. formatting dates or omitting unnecessary properties.
但是可能更好的是向模型/实体添加toArray()
- 或toJson()
- 方法。这样你就不需要额外的代码来检索你的实体,你可以在将模型的数据作为 JSON 传递之前更改它,例如格式化日期或省略不必要的属性。
回答by davmor
Have done this two ways if you are working with a single entity:
如果您正在使用单个实体,请通过两种方式完成此操作:
$hydrator = new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($entityManager);
$entityArray = $hydrator->extract($entity);
Last resort, you can just cast to an array like any other PHP object via:
最后的手段,您可以像任何其他 PHP 对象一样通过以下方式强制转换为数组:
$array = (array) $entity
NB: This may have unexpected results as you are may be working with doctrine proxy classes, which may also change in later Doctrine versions..
注意:这可能会产生意想不到的结果,因为您可能正在使用学说代理类,这也可能在以后的教义版本中发生变化。
回答by Atul Pandya
Can also be used:
$query = $em->createQueryBuilder()
->select('p')
->from('Products', 'p')
->where('p.BarCode = :barcode')
->setParameter('barcode', $valur)
->getQuery()
;
$data = $query->getArrayResult();