Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite serializer metadata for DoctrineExtensions Taggable

Can I overwrite the way the tag object gets serialized? Currently everything is returned, I'd like to exclude the id, created_at, updated_at and tagging. Im using the JMS Serializer bundle, Doctrine Extensions Taggable with FPN Tag Bundle.

This is my setup, I'm thinking setting the parent of the Tag Bundle to FPN when the namespace of the Entity is actually DoctrineExtensions could be the issue.

Most the entity parameters are in DoctrineExtensions\Taggable\Entity\Tag (id,name,created_at etc). I'm overwriting the FPN bundle which extends DoctrineExtensions. DoctrineExtensions is a library not a bundle.

How can I do this?

# 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';
    }
}
like image 230
shapeshifter Avatar asked Nov 11 '22 08:11

shapeshifter


1 Answers

JMSSerializer requires you to define your serialization configuration on the same namespace as the one where the properties are declared.

For instance, let's say you have a Application\Bundle\AcmeBundle\Entity\BaseModel class with $createdAt and $updatedAt properties, and a Application\Bundle\AcmeBundle\Entity\Model class inheriting the BaseModel class with a $name property. In that case, you'll need 2 serialization files: one named Entity.BaseModel.xml with the serialization config for $createdAt and $updatedAt properties; and one named Entity.Model.xml with the config for the $name property.

You overrode the configuration for the FPNTagBundle well, however the only field you may configure for serialization with your current configuration is the $slug field (which is defined in the FPN\TagBundle\Entity\Tag class). For the other fields, you'll need to override the configuration directories of DoctrineExtensions\Taggable\Entity\Tag.

Your config should then be something like this:

# 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 :)
like image 136
Hugo Briand Avatar answered Nov 15 '22 13:11

Hugo Briand