[英]Symfony project - Doctrine Self-Reference issue
我正在做一個 Symfony 6.1 項目,遇到以下情況。 我有一個名為“MaterialGroup”的實體。 一個材質組可以和它自己成父子關系,一個材質組可以有多個“子組”,一個子組只能屬於一個“上層組”。 所以基本上它是一對多關系。
現在,當我想更新 symfony 中 controller 的“新建”或“編輯”function 中的關系時,它基本上不起作用。 我沒有收到任何錯誤,如果我使用 function“dd()”來預覽父材料組,我可以看到應該映射的子組。 然而,沖洗后什么也沒有發生。
這是我當前的代碼:
實體
class MaterialGroup
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=MaterialGroup::class, inversedBy="subGroups")
*/
private $parentMaterialGroup;
/**
* @ORM\OneToMany(targetEntity=MaterialGroup::class, mappedBy="parentMaterialGroup")
*/
private $subGroups;
public function __construct()
{
$this->subGroups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getParentMaterialGroup(): ?self
{
return $this->parentMaterialGroup;
}
public function setParentMaterialGroup(?self $parentMaterialGroup): self
{
$this->parentMaterialGroup = $parentMaterialGroup;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getSubGroups(): Collection
{
return $this->subGroups;
}
public function addSubGroup(self $subGroup): self
{
if (!$this->subGroups->contains($subGroup)) {
$this->subGroups[] = $subGroup;
$subGroup->setParentMaterialGroup($this);
}
return $this;
}
public function removeSubGroup(self $subGroup): self
{
if ($this->subGroups->removeElement($subGroup)) {
// set the owning side to null (unless already changed)
if ($subGroup->getParentMaterialGroup() === $this) {
$subGroup->setParentMaterialGroup(null);
}
}
return $this;
}
}
在 Controller 中編輯 function
#[Route('/{id}/edit', name: 'edit', methods: ['GET', 'POST'])]
public function edit(Request $request, MaterialGroup $materialGroup, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(MaterialGroupType::class, $materialGroup);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
$subGroups = [];
if(isset($data["material_group"]["subGroups"]))
$subGroups = $data["material_group"]["subGroups"];
//Get the material group objects for all the subGroups
$requestSubGroups = [];
foreach($subGroups as $value) {
$requestSubGroups[] = $this->materialGroupRepository->find($value);
}
//"Insert" the new subGroup relations
foreach($requestSubGroups as $subGroup) {
$materialGroup->addSubGroup($subGroup);
}
//This dd() leads to the object you can see after the code for the edit function in this post
dd($materialGroup);
$this->entityManager->flush();
return $this->redirectToRoute($this->routeName.'_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm($this->routeName.'/edit.html.twig', [
$this->routeName => $materialGroup,
'form' => $form,
]);
}
這個 object 顯示了在刷新之前應該映射到這個 materialGroup 的子組。 在刷新之后它們沒有被映射:
App\Entity\Tables\MaterialGroup {#1116 ▼
-id: 982
-parentMaterialGroup: null
-subGroups: Doctrine\ORM\PersistentCollection {#1324 ▼
#collection: Doctrine\Common\Collections\ArrayCollection {#1335 ▼
-elements: array:5 [▼
0 => App\Entity\Tables\MaterialGroup {#1707 ▶}
1 => App\Entity\Tables\MaterialGroup {#1729 ▶}
2 => App\Entity\Tables\MaterialGroup {#1750 ▶}
3 => App\Entity\Tables\MaterialGroup {#1771 ▶}
4 => App\Entity\Tables\MaterialGroup {#1885 ▶}
]
}
#initialized: true
-snapshot: array:4 [ …4]
-owner: App\Entity\Tables\MaterialGroup {#1116}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#452 …11}
-backRefFieldName: "parentMaterialGroup"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1009 …}
-isDirty: true
}
}
誰能告訴我我在這里誤解或做錯了什么?
在獲取子組后嘗試使用 setParentMaterialGroup function 而不是 addSubGroup:
$subGroup->setParentMaterialGroup($materialGroup);
誰能告訴我我在這里誤解或做錯了什么?
這是:
材料組可以與自身處於父子關系
如果你想要關系,你應該為它們之間的設定關系創建不同的實體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.