Symfony/Doctrine:如何将一个实体序列化到一个键值数组
问题描述:
由于某种原因,我想将一个实体序列化为一个简单的key => value数组而不包含相关的实体以及数据库中列的真实名称。有没有办法开箱即用?或者我应该使一个实体序列化程序使用注释来加载列名称Symfony/Doctrine:如何将一个实体序列化到一个键值数组
答
我相信这可能是您的解决方案的一部分。
$reader = new AnnotationReader();
$fieldNames = $em->getClassMetadata('AppBundle:Entity')->getFieldNames();
foreach($fieldNames as $field){
$reflClass = new \ReflectionProperty('AppBundle\Entity\Entity',$field);
$classAnnotations = $reader->getPropertyAnnotations($reflClass);
var_dump($classAnnotations[0]->name);
}
你可能如果属性名称,而不是列名是适合你使用标准的串行器组件。 –