0
votes

I have configured Symfony 3 to auto serialize my action. It works, but the relations aren't serialized:

0   
    id  1
    name    "Liste de course"
    creation_date   "2017-07-07T00:00:00+00:00"
    last_update_date    "2017-07-07T20:57:06+00:00"
    user_entity 
    _todo_entity_entities   
    _parent_entity  
1   
    id  2
    name    "domotique"
    creation_date   "2017-07-07T00:00:00+00:00"
    last_update_date    "2017-07-07T21:22:52+00:00"
    user_entity 
    _todo_entity_entities   
    _parent_entity

If I explicitly use JMSSerializerBundle, it works (user_entity is an object):

0   
    id  1
    name    "Liste de course"
    creation_date   "2017-07-07T00:00:00+00:00"
    last_update_date    "2017-07-07T20:57:06+00:00"
    user_entity Object
    _todo_entity_entities   
1   
    id  2
    name    "domotique"
    creation_date   "2017-07-07T00:00:00+00:00"
    last_update_date    "2017-07-07T21:22:52+00:00"
    user_entity Object
    _todo_entity_entities

I think FOSRestBundle uses the default seralizer, not JMSSerializerBundle:

/**
 * @Rest\Get("/projects")
 * @View(
 *    serializerGroups = {"all"}
 * )
 */
public function getProjectsAction()
 {
     $projectEntity = $this->getDoctrine()->getRepository('todoListAdminBundle:Project');
     $projects = $projectEntity->findAll();
     /*
     $data = $this->get('jms_serializer')->serialize($projects, 'json');
     // this is work !
     $response = new Response($data);
     $response->headers->set('Content-Type', 'application/json');
     return $response;
    */

     return $projects;
 }

The entity I serialize :

/**
 * Project
 *
 * @ORM\Table(name="project")
 *             @ORM\Entity(repositoryClass="todoListAdminBundle\Repository\ProjectRepository")
 */
class Project
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
  * @Serializer\Groups({"all"})
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
  * @Assert\Length(max=50)
  * @Assert\Type(type="string")
  * @Serializer\Groups({"all"})
 */
private $name;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="creation_date", type="date")
  * @Assert\DateTime()
  * @Serializer\Groups({"all"})
 */
private $creationDate;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="last_update_date", type="datetime")
  * @Assert\DateTime()
  * @Serializer\Groups({"all"})
 */
private $lastUpdateDate;

/**
 *
 * @ORM\ManyToOne(targetEntity="PW\UserBundle\Entity\User", inversedBy="projectEntities" , cascade={"persist"}, inversedBy="projectEntities")
  * @Assert\Type(type="integer")
  * @Serializer\Groups({"all"})
 */
private $userEntity;

/**
 * @ORM\OneToMany(targetEntity="todoListAdminBundle\Entity\TodoEntity", mappedBy="projectEntity", fetch="EAGER")
 * @Serializer\Groups({"all"})
 */
private $TodoEntityEntities;

/**
 * @var int
 *
 * @ORM\JoinColumn(nullable=true, referencedColumnName="id")
 * @ORM\OneToOne(targetEntity="todoListAdminBundle\Entity\Project")
 * @Assert\Type(type="integer")
 * @Serializer\Groups({"all"})
 */
private $ParentEntity;

My configuration :

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    zone:
        - { path: ^/api/* }
    body_converter:
        enabled: true
    view:
        formats: { json: true, xml: false, rss: false }
        view_response_listener: true
    serializer:
        serialize_null: true
    format_listener:
        enabled: true
        rules:
            - { path: '^/api', priorities: ['json'], fallback_format: 'json' }
    routing_loader:
        default_format: json

sensio_framework_extra:
    view: { annotations: true }

How can I use JMSSerializerBundle automatically ?

1

1 Answers

0
votes

First of all, you need to configure JMSSerializer in your config.yml like:

jms_serializer:
    metadata:
        cache: file
        debug: "%kernel.debug%"
        file_cache:
            dir: "%kernel.cache_dir%/serializer"
        auto_detection: true 

Then, create directory with serializer for the given entity YourBundleName/Resources/config/serializer/Entity.Project.yml with this code:

YourBundleName\Entity\Project:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        name:
            expose: true

"exclusion_policy: ALL" - exclude all the fields from the serialized result. And then you add needed fields with "expose: true". Just do not add "ParentEntity" there and you will not see it in the serialized data (also, I do not think, that mix of camel and pascal case is a good practice, but it's the question of taste).