Symfony Serializer 不序列化 OneToOne 关系 / Doctrine Proxy
Symfony Seralizer doesn't serialize OneToOne relation / Doctrin Proxy
我正在尝试序列化与图像实体具有一对一关系的实体。
转储实体时,我可以看到图像实体是 Doctrine Proxy 并且未初始化。
在尝试访问端点时,它也不会出现在 JSON 响应中。
我试过 fetch="EAGER" 但没有成功。
如何将其序列化为“预期响应”?
回复:
[
{
"id": 7
}
]
预期响应:
[
{
"id": 7,
"image": {
"webView": "someUrl"
}
}
]
实体:
/**
* @ORM\Entity(repositoryClass=BatteryTypeRepository::class)
*/
class BatteryType
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @Groups({"type"})
*/
private $id;
/**
* @ORM\OneToOne(targetEntity=Image::class, cascade={"persist", "remove"})
*
* @Groups({"type"})
*/
private $image;
/**
* @return Image|null
*/
public function getImage(): ?Image
{
return $this->image;
}
public function setImage(?Image $image): self
{
$this->image = $image;
return $this;
}
}
图像实体:
/**
* @ORM\Entity(repositoryClass=ImageRepository::class)
* @ORM\EntityListeners({"App\EventSubscriber\ImageListener"})
*/
class Image
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"type"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $filename;
/**
* Auto generated by entity listener
* @var string
*/
private $tempFilename;
/**
* @var File
* @Assert\Image()
* @Assert\NotBlank()
*/
private $file;
/**
* @var string
* @Groups({"type"})
*/
private $webView;
/** Getters & Setters */
}
图像监听器:
class ImageListener
{
/**
* @var S3FileManagerInterface
*/
private S3FileManagerInterface $fileManager;
public function __construct(S3FileManagerInterface $fileManager)
{
$this->fileManager = $fileManager;
}
public function postLoad(Image $image, LifecycleEventArgs $args): void
{
$image->setWebView(
$this->fileManager->getUrl($image->getFilename())
);
if (!$image->getTempFilename()) {
$image->setTempFilename($image->getFilename());
}
}
}
控制器:
public function getBatteryTypes(BatteryTypeRepository $batteryTypeRepository): Response
{
$batteryTypes = $batteryTypeRepository->findAll();
$view = $this->view($batteryTypes, Response::HTTP_OK);
$view->getContext()->setGroups(["type"]);
return $this->handleView($view);
}
在您的 Image
实体中,您必须为 webView
属性添加 @Groups({"type"})
注释。
我的问题显然只是缓存相关
我正在尝试序列化与图像实体具有一对一关系的实体。 转储实体时,我可以看到图像实体是 Doctrine Proxy 并且未初始化。
在尝试访问端点时,它也不会出现在 JSON 响应中。 我试过 fetch="EAGER" 但没有成功。
如何将其序列化为“预期响应”?
回复:
[
{
"id": 7
}
]
预期响应:
[
{
"id": 7,
"image": {
"webView": "someUrl"
}
}
]
实体:
/**
* @ORM\Entity(repositoryClass=BatteryTypeRepository::class)
*/
class BatteryType
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @Groups({"type"})
*/
private $id;
/**
* @ORM\OneToOne(targetEntity=Image::class, cascade={"persist", "remove"})
*
* @Groups({"type"})
*/
private $image;
/**
* @return Image|null
*/
public function getImage(): ?Image
{
return $this->image;
}
public function setImage(?Image $image): self
{
$this->image = $image;
return $this;
}
}
图像实体:
/**
* @ORM\Entity(repositoryClass=ImageRepository::class)
* @ORM\EntityListeners({"App\EventSubscriber\ImageListener"})
*/
class Image
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"type"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $filename;
/**
* Auto generated by entity listener
* @var string
*/
private $tempFilename;
/**
* @var File
* @Assert\Image()
* @Assert\NotBlank()
*/
private $file;
/**
* @var string
* @Groups({"type"})
*/
private $webView;
/** Getters & Setters */
}
图像监听器:
class ImageListener
{
/**
* @var S3FileManagerInterface
*/
private S3FileManagerInterface $fileManager;
public function __construct(S3FileManagerInterface $fileManager)
{
$this->fileManager = $fileManager;
}
public function postLoad(Image $image, LifecycleEventArgs $args): void
{
$image->setWebView(
$this->fileManager->getUrl($image->getFilename())
);
if (!$image->getTempFilename()) {
$image->setTempFilename($image->getFilename());
}
}
}
控制器:
public function getBatteryTypes(BatteryTypeRepository $batteryTypeRepository): Response
{
$batteryTypes = $batteryTypeRepository->findAll();
$view = $this->view($batteryTypes, Response::HTTP_OK);
$view->getContext()->setGroups(["type"]);
return $this->handleView($view);
}
在您的 Image
实体中,您必须为 webView
属性添加 @Groups({"type"})
注释。
我的问题显然只是缓存相关