无法在schmittjoh / serializer中序列化虚拟属性

时间:2015-02-16 23:30:43

标签: php jmsserializerbundle jms-serializer

我序列化的一个类有一个我想看的虚拟属性。我正在使用yaml配置。

这是yaml:

Namespace\Model\Keg:
   exclusion_policy: ALL
   properties:
      A list of properties that work
   virtual_properties:
      getKegImage:
         serialized_name: image

这是班级:

namespace Namespace\Model;

class Keg extends ModelAbstract
{
       /** Some properties + accessors **/

       /**
       * @param bool $asImgTag Whether or not to return the URL in an <img> tag
       * @return array
       */
       public function getKegImage ($asImgTag = false)
       {
               return [
                    'Key' => 'value',
                    'Key' => 'value',
                    'Key' => 'value'
               ];
       }
}

当我序列化模型时,我希望在json中看到一个以数组为值的图像字段。相反,该领域缺失。我在yaml文件中尝试了很多设置组合,但我无法显示它。

2 个答案:

答案 0 :(得分:0)

从查看代码看,使用yaml元数据看起来不支持虚拟属性。幸运的是,如果需要,您可以混合使用元数据源。我只是将@VirtualProperty Doctrine注释添加到方法中,它按预期工作。

答案 1 :(得分:0)

到目前为止,YAML支持虚拟属性。

对于其他Google员工,如果您使用的是groups,请确保也将其列出在虚拟属性定义中,即使正式的YAML参考并未将其列出为可能的值

相关问题