2

我正在序列化的一个类有一个我想看到的虚拟属性。我正在使用 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 文件中尝试了许多设置组合,但无法显示。

4

2 回答 2

0

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

于 2015-02-19T14:41:04.393 回答
0

YAML 目前支持虚拟属性。

对于 Google 同事,如果您正在使用groups,请确保将它们也列在虚拟属性定义中,即使官方 YAML 参考并未将它们列为可能的值

于 2018-11-06T14:54:25.073 回答