我正在做一个Symfony 6.1项目,有一个实体叫"物料组",一个物料组可以和自己有父子关系,一个物料组可以有几个"子组",一个子组只能属于一个"上级组",所以基本上是一对多关系。
现在,当我想在symfony中的控制器的"新建"或"编辑"功能中更新关系时,它基本上不起作用。我没有得到任何错误,如果我使用函数"dd()"来预览父materialGroup,我可以看到应该Map的子组。但是在刷新后什么也没有发生。
下面是我目前的代码:
- 实体**
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;
}
}
- 在控制器中编辑功能**
#[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,
]);
}
此对象显示了在刷新之前应Map到此materialGroup的子组。刷新之后,它们未被Map:
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
}
}
有谁能告诉我,我在这里误解了什么或做错了什么?
2条答案
按热度按时间2ekbmq321#
获取子组后,尝试setParentMaterialGroup函数而不是addSubGroup:
a14dhokn2#
有谁能告诉我,我在这里误解了什么或做错了什么?
这是:
物料组可以与自身存在父子关系
如果你想要的关系,你应该创建不同的实体之间的设置关系。