简体   繁体   English

从对象到数组在doctrine2中

[英]From object to array in doctrine2

I'm using Doctrine 2 ORM to map my SQL table into objects. 我正在使用Doctrine 2 ORM将SQL表映射到对象中。 Is there a support method like asArray which converts my objects into an array? 是否有诸如asArray类的支持方法将我的对象转换为数组?

You can simply use the getArrayResult method that is mentioned here inside the Doctrine 2 documentation chapter 14.7.4.2. 您可以简单地使用Doctrine 2文档第14.7.4.2章中提到的getArrayResult方法 Array Hydration : 阵列水化

$array = $query->getArrayResult();

It is a short notation for, and does exactly the same as, this answer that that @Joucks is pointing to in his comment : 这是@Joucks在其评论中指向的答案的简称,并且与之完全相同:

$array = $query->getResult(Query::HYDRATE_ARRAY);

But you don't necessarily have to look at specific Doctrine ORM solutions for such serializing functionality. 但是您不必一定要针对此类序列化功能查看特定的Doctrine ORM解决方案。 You could also look at solutions that come with the PHP framework that you are using. 您还可以查看所使用的PHP框架随附的解决方案。

Zend-Framework Zend框架

For example if you use Zend-Framework you could implement the ArraySerializableInterface inside the objects that you want to be able to serialize to an array. 例如,如果您使用Zend-Framework,则可以想要序列化到数组的对象内部实现ArraySerializableInterface

This interface includes an getArrayCopy method that should return the object serialized to an array. 此接口包括getArrayCopy方法,该方法应将序列化为数组的对象返回。

You can read more about ArraySerializable inside the Zend-Framework 2 Zend\\Stdlib documentation 您可以在Zend-Framework 2 Zend\\Stdlib文档中阅读有关ArraySerializable更多信息。

Symfony Symfony

Symfony ships with this Serializer component . Symfony附带此Serializer组件


For other frameworks I expect they are shipped with similar interfaces/solutions. 对于其他框架,我希望它们附带类似的接口/解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM