5

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';
    }
}
shapeshifter
  • 2,967
  • 2
  • 25
  • 39
  • `expose: true` (for the name property) doesn't seem to be indented properly. Is that a typo in your question, or in your actual yaml file? – Nic Wortel May 05 '14 at 08:01
  • Also, can you add the following to one of the properties, to check if the config is actually being used at all? `serialized_name: 'fooBar'` – Nic Wortel May 05 '14 at 08:03
  • Yeah that was a typo i've fixed it up. Symfony should have thrown a yaml parse error on that if it was wrongly indented. I'll change the serialized_name shortly. – shapeshifter May 05 '14 at 09:27
  • Looks like its not even being loaded. – shapeshifter May 06 '14 at 00:40
  • So I figured it out, I changed the config line, namespace_prefix: DoctrineExtensions\Taggable. Removed the quotes and the escaped slash now it works. Even the JMS docs have quotes and double slashes. – shapeshifter May 06 '14 at 00:54

1 Answers1

1

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 :)
Hugo Briand
  • 1,683
  • 20
  • 27