Mak*_*ari 5 symfony doctrine-orm
在一个简单的 Symfony 项目中,我创建了两个实体Product和,它们通过和与 Doctrine Annotations 的关系Category相关。一个类别可以有多个产品,一个产品与一个类别相关。我已在表中手动插入数据。@ManyToOne@OneToManyCategory
当我使用Category实体存储库获取数据并使用 a 显示它时var_dump(...),会发生无限递归。当我返回带有这些数据的 JSON 响应时,它只是空的。它应该准确检索我手动插入的数据。
您是否知道如何在不删除Category实体中的反向关系的情况下避免此错误?
Category使用 Doctrine 在数据库中插入对象以查看数据库连接是否正常工作。是的。控制器
dummy/src/Controller/DefaultController.php
...
$entityManager = $this->getDoctrine()->getManager();
$repository = $entityManager->getRepository(Category::class);
// ===== PROBLEM HERE =====
//var_dump($repository->findOneByName('house'));
//return $this->json($repository->findOneByName('house'));
...
Run Code Online (Sandbox Code Playgroud)
实体
dummy/src/Entity/Category.php
<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CategoryRepository::class)
*/
class Category
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(name="id", type="integer")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=Product::class, mappedBy="category", fetch="LAZY")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
// set the owning side to null (unless already changed)
if ($product->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
dummy/src/Entity/Product.php
<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(name="id", type="integer")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="products", fetch="LAZY")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
我假设您用于var_dump调试目的。出于调试目的,请使用dump或ddwhich 来自symfony/debug并且默认情况下应该已启用dev。和dump都dd 应该及时中止无限递归。(很多 symfony/doctrine 对象/服务都有循环引用,或者只是很多引用的对象。)dump将给定的 php var(s) 添加到探查器(探查器栏中的目标标记符号)或输出。dd添加给定的变量dump,但也结束该过程(所以转储和死亡)。- 在生产中切勿使用 dump/dd/var_dump,而是正确序列化数据。
其次,本质上是打包到对象中$this->json的快捷方式(或使用 symfony/序列化器代替)。另一方面序列化公共属性json_encodeJsonResponsejson_encode除非对象实现(见下文),否则序列化给定对象的JsonSerializable。由于几乎所有实体通常都将其所有属性设为私有,因此结果通常是空对象序列化。
有多种选项可供选择,但本质上你需要解决无限递归的问题。恕我直言,标准选项是:
JsonSerializable并小心避免递归地添加子对象。$this->json数组,以传递给(“手动方法”)。A数组是指仅包含字符串、数字以及字符串和数字的(嵌套)数组,这本质上意味着丢失所有实际对象。
可能还有其他选择,但我发现这些是最方便的。我通常更喜欢JsonSerializable选择,但这是一个品味问题。一个例子是:
class Category implements \JsonSerializable { // <-- new implements!
// ... your entity stuff
public function jsonSerialize() {
return [
'id' => $this->id,
'name' => $this->name,
'products' => $this->products->map(function(Product $product) {
return [
'id' => $product->getId(),
'name' => $product->getName(),
// purposefully excluding category here!
];
})->toArray(),
];
}
}
Run Code Online (Sandbox Code Playgroud)
添加此代码后,您的代码应该可以正常工作。对于开发人员,您始终应该dump按照提到的方式使用,一切$this->json都会正常工作。这就是为什么我通常更喜欢这个选项。但是,需要注意的是:通过这种方式,您只能为类别提供一种json 序列化方案。对于任何其他方式,您将不得不使用其他选项......无论如何,这几乎总是正确的。
| 归档时间: |
|
| 查看次数: |
1383 次 |
| 最近记录: |