5

我可以覆盖标签对象的序列化方式吗?目前一切都返回了,我想排除 id、created_at、updated_at 和标记。我正在使用 JMS Serializer 包,Doctrine Extensions Taggable with FPN Tag Bundle。

这是我的设置,当实体的命名空间实际上是 DoctrineExtensions 时,我正在考虑将 Tag Bundle 的父级设置为 FPN。

大多数实体参数位于 DoctrineExtensions\Taggable\Entity\Tag(id、name、created_at 等)中。我正在覆盖扩展 DoctrineExtensions 的 FPN 包。DoctrineExtensions 是一个库而不是一个包。

我怎样才能做到这一点?

# app/config/config.yml
# ...
jms_serializer:
    metadata:
        auto_detection: true
        directories:
            TagBundle:
                namespace_prefix: "FPN\\TagBundle"
                path: "@MYTagBundle/Resources/config/serializer/fpn"


# MY\TagBundle\Resources\config\serializer\fpn\Entity.Tag.yml
FPN\TagBundle\Entity\Tag:
    exclusion_policy: ALL
    properties:
        id:
            expose: false
        name:
            expose: true
        created_at:
            expose: false
        updated_at:
            expose: false
        tagging:
            expose: false


# src/MY/TagBundle/Entity/Tag.php
<?php
namespace MY\TagBundle\Entity;

use FPN\TagBundle\Entity\Tag as BaseTag;

class Tag extends BaseTag
{
}


# vendor/fpn/tag-bundle/FPN/TagBundle/Entity/Tag.php
<?php

namespace FPN\TagBundle\Entity;

use DoctrineExtensions\Taggable\Entity\Tag as BaseTag;

class Tag extends BaseTag
{
    ....
}



# src/MY/TagBundle/MYTagBundle.php
<?php

namespace MY\TagBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MYTagBundle extends Bundle
{
    // Is this unnecessary due to config.yml?
    public function getParent()
    {
        return 'FPNTagBundle';
    }
}
4

1 回答 1

1

JMSSerializer 要求您在与声明属性的名称空间相同的名称空间上定义您的序列化配置。

例如,假设您有一个Application\Bundle\AcmeBundle\Entity\BaseModel具有$createdAt$updatedAt属性的Application\Bundle\AcmeBundle\Entity\Model类,以及一个继承BaseModel具有$name属性的类的类。在这种情况下,您需要 2 个序列化文件:一个以Entity.BaseModel.xml序列化配置$createdAt$updatedAt属性命名;一个以Entity.Model.xml$name属性的配置命名。

您很好地覆盖了 FPNTagBundle 的配置,但是您可以使用当前配置为序列化配置的唯一字段是$slug字段(在FPN\TagBundle\Entity\Tag类中定义)。对于其他字段,您需要覆盖DoctrineExtensions\Taggable\Entity\Tag.

你的配置应该是这样的:

# app/config/config.yml
# ...
jms_serializer:
    metadata:
        auto_detection: true
        directories:
            TagBundle:
                namespace_prefix: "FPN\\TagBundle"
                path: "@MYTagBundle/Resources/config/serializer/fpn"
            DoctrineTaggable:
                namespace_prefix: "DoctrineExtensions\\Taggable"
                path: "@MYTagBundle/Resources/config/serializer/doctrine"


# MY\TagBundle\Resources\config\serializer\fpn\Entity.Tag.yml
FPN\TagBundle\Entity\Tag:
    exclusion_policy: ALL
    properties:
        id:
            expose: false
        name:
            expose: true
        created_at:
            expose: false
        updated_at:
            expose: false
        tagging:
            expose: false
# MY\TagBundle\Resources\config\serializer\fpn\Entity.Tag.yml
FPN\TagBundle\Entity\Tag:
    exclusion_policy: ALL
    properties:
        slug:
            expose: false # or true, as you wish :)
于 2014-05-05T12:02:05.237 回答