sha*_*ter 5 php symfony doctrine-extensions
我可以覆盖标记对象序列化的方式吗?目前返回所有内容,我想排除id,created_at,updated_at和标记.我正在使用JMS Serializer软件包,Doctrine Extensions Taggable和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';
}
}
Run Code Online (Sandbox Code Playgroud)
JMSSerializer 要求您在与声明属性的命名空间相同的命名空间中定义序列化配置。
例如,假设您有一个具有和属性Application\Bundle\AcmeBundle\Entity\BaseModel的类,以及一个继承具有属性的类的类。在这种情况下,您将需要 2 个序列化文件:一个以序列化配置和属性命名;另一个以序列化配置命名。以及一个以属性配置命名的。$createdAt$updatedAtApplication\Bundle\AcmeBundle\Entity\ModelBaseModel$nameEntity.BaseModel.xml$createdAt$updatedAtEntity.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 :)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
414 次 |
| 最近记录: |