Symfony 4 : Use MaxDepth annotation of jms serializer

I send a get http request from angular side to symfony to get command details, I have many nested json objects and Circular Reference, I’m trying to set MaxDepth to the related entity and as the documentation of jms said add this line

$serializer->serialize($command, 'json', SerializationContext::create()->enableMaxDepthChecks());
to create a serializer context and enable the maxDepth property

This my entity

<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use JMSSerializerAnnotationMaxDepth;

/**
 * @ORMEntity(repositoryClass="AppRepositoryCommandRepository")
 */
class Command
{
    /**
     * @ORMId()
     * @ORMColumn(type="string", length=255)
     */
    private $id;

    /**
     * @ORMColumn(type="float")
     */
    private $price;


    /**
     * @ORMColumn(type="text")
     */
    private $adresse;

    /**
     * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="commands")
     * @ORMJoinColumn(nullable=true, onDelete="CASCADE")
     */
    private $user;

    /**
     * @ORMOneToMany(targetEntity="AppEntityCommandLine", mappedBy="command")
     */
    private $commandLines;


    /**
     * @ORMOneToMany(targetEntity="AppEntityGiftCheck", mappedBy="command")
     * @MaxDepth(1)
     */
    private $giftChecks;

Method of command details

/**
     * Get Commands.
     * @RestGet("/CommandDetails/{id}")
     * @param Request $request
     * @return View
     */

    public function CommandDetails( Request $request,$id)
    {
      $entityManager = $this->getDoctrine()->getManager();
      $command = $this->getDoctrine()->getRepository(Command::class)->find($id);
      $command-> setUser($this->getUserDetails(   $command-> getUser() ) );

      $serializer->serialize($command, 'json', SerializationContext::create()->enableMaxDepthChecks());
      return $this->handleView($this->view($command));
    }

it seems normal that the server give a 500 error cause didn’t know the $serializer variable cause it’s not initialized, I didn’t found how to initialize this variable form what interface ! some help please ?

followed link

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *