2017-07-07 5 views
2

J'ai une entité Symfony 3.2 utilisant Doctrine's Translatable extension.Sérialiseurs JSON personnalisés pour une entité Symfony 3

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; 

    /* [...] */ 
} 

Je sais comment utiliser le de base façon de sérialiser cette entité JSON (j'utilise friendsofsymfony/rest-bundle pour la sérialisation de mon API REST), mais je voudrais sérialisation d'une manière personnalisée comme ce ...

{ 
    "description": { 
     "fr": "description en français", 
     "en": "english description", 
    } 
} 

Répondre

0

essayez ceci:

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; 

    /* [...] */ 
}