我正在尝试创建一个控制器方法来编辑现有的实体 UserGroup,但只修改实体 User 的 ManyToMany 关系字段,这是 FormType 中唯一的字段。问题是,
我正在尝试创建一个控制器方法来编辑现有实体 UserGroup
,但只修改 ManyToMany
与实体的关系字段 User
,这是 FormType 中唯一的字段。
问题是,虽然 POST 数据已被发送,并且检查实体后 handleRequest()
显示关系字段已被正确捕获,但什么都没有保存。
注意 :使用 Symfony 生成的 CRUD 控制器也不会保存这个关系字段,它会保存其他字段但不保存关系字段,所以关系定义中可能有问题。
这是 UserGroup
实体:
#[ORM\Entity(repositoryClass: UserGroupRepository::class)]
class UserGroup extends ItemCollection implements IMessageInternalRecipient
{
const USER_GROUP_MARKETING = 'marketing';
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'groups', cascade: ['persist', 'remove'])]
private Collection $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/*
insert into user_group(view_order, shortname, name) values(1, 'marketing', 'Equipo de marketing');
*/
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): static
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->addGroup($this);
}
return $this;
}
public function removeUser(User $user): static
{
if ($this->users->removeElement($user)) {
$user->removeGroup($this);
}
return $this;
}
}
这是 ItemCollection
(前一个实体从其扩展而来):
#[ORM\MappedSuperclass]
class ItemCollection
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
protected $id;
#[ORM\Column(type: 'string', length: 100)]
protected $shortname;
//... and other fields
}
这是 User
实体中的关系:
#[ORM\ManyToMany(targetEntity: UserGroup::class, inversedBy: 'users')]
private Collection $groups;
UserGroupType
:
class UserGroupType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('users', EntityType::class, [
'label' => 'Marketing users',
'class' => User::class,
'choice_label' => 'email',
'multiple' => true,
'expanded' => true
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => UserGroup::class,
]);
}
}
这是控制器方法。注意:我没有传递实例 $userGroup
对该简称 find()
执行了操作
#[Route('/group_users/{shortname}', name: 'app_user_group_users', methods: ['GET', 'POST'])]
public function groupUsers(Request $request,
EntityManagerInterface $entityManager,
UserGroupRepository $userGroupRepository,
LoggerInterface $logger,
string $shortname): Response
{
$userGroup = $userGroupRepository->findOneBy(['shortname' => $shortname]);
if ($userGroup == null)
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
$form = $this->createForm(UserGroupType::class, $userGroup,
['action' => $this->generateUrl('app_user_group_users', ['shortname' => $shortname])]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
// $entityManager->persist($userGroup); //This was tried but didn't work either
$entityManager->flush();
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('user/group_users.html.twig', [
'user_group' => $userGroup,
'form' => $form,
]);
}