Symfony 4 串行器组件 - MaxDepth 不起作用

Vla*_*scu 5 serialization symfony4

我正在尝试序列化一个实体,但我不希望序列化程序获取关联的实体关联。

我有一个具有关联国家/地区的财产实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\MaxDepth;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
 * @Gedmo\Loggable
 */
class Property
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
protected $id;

/**
 * @ORM\Column(type="string")
 * @Gedmo\Versioned
 */
protected $name;

/**
 * @ORM\ManyToOne(targetEntity="Country", inversedBy="properties", fetch="EXTRA_LAZY")
 * @Gedmo\Versioned
 * @MaxDepth(1)
 */
protected $country;

.....
Run Code Online (Sandbox Code Playgroud)

Country 实体具有 oneToMany City 关联。

然后,当我序列化 Property 实体时

// PropertyService class

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($normalizer));

$normalizer->setCircularReferenceLimit(0);
$normalizer->setCircularReferenceHandler(function ($object) {
    return $object->getId();
});

$result = $serializer->normalize($property, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
Run Code Online (Sandbox Code Playgroud)

$result 是一个规范化 Property 的数组,其中包含关联的 Country 及其所有城市。我预计 @MaxDepth(1) 注释将停止获取该国家/地区的关联城市。

我怎样才能做到这一点?

Bar*_*arh 0

enable_max_depth仅当序列化器上下文的键设置为 时才会进行检查true