6

I have a Symfony 3.2 entity using Doctrine's Translatable extension.

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Media
 *
 * @ORM\Table(name="medias")
 * @Gedmo\TranslationEntity(class="AppBundle\Entity\Translation\MediaTranslation")
 */
class Media implements Translatable
{
    /* [...] */
    
    /**
     * @var string|null
     *
     * @ORM\Column(type="text", nullable=true)
     * @Gedmo\Translatable
     * @Groups({"single_media"})
     */
    private $description;

    /* [...] */
}

I know how to use the basic way to serialize this entity in JSON (I use friendsofsymfony/rest-bundle for the serialization of my REST API), but I would like to serialize it in a custom way like this...

{
    "description": {
        "fr": "description en français",
        "en": "english description",
    }
}
Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
Damien Debin
  • 2,812
  • 25
  • 41

1 Answers1

0

try this :

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Media
 *
 * @ORM\Table(name="medias")
 * @Gedmo\TranslationEntity(class="AppBundle\Entity\Translation\MediaTranslation")
 */
class Media implements Translatable, \Serializable
{
    /* [...] */

    /**
     * @var string|null
     *
     * @ORM\Column(type="text", nullable=true)
     * @Gedmo\Translatable
     * @Groups({"single_media"})
     */
    private $description;

    /* [...] */
}
walidtlili
  • 840
  • 2
  • 13
  • 36