我正在尝试序列化实体,但我不希望序列化程序获取关联的etities关联。
我有一个与相关国家/地区相关的财产实体:
<?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;
.....
“国家/地区”实体具有“一对多城市”协会。
然后,当我序列化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));
$ result是包含相关国家/地区及其所有城市的规范化属性的数组。我希望@MaxDepth(1)注释将停止获取该国家/地区的关联城市。
我该怎么做?