表单正在替换我在 Symfony 4 中的变量
Form is replacing my variable in Symfony 4
我想更新一个名为 SAI 的实体的用户列表,但是在提交表单后,我丢失了我要更新的 SAI 的信息,它被表单返回的 SAI 所取代。
我试图在以下代码中解释评论会发生什么:
EdditSAIController.php
/**
* @Route("/edit_sai/{id}/", name="edit_sai", requirements={"page"="\d+"}))
* @Method({"GET"})
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editSAI(Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$idSAI = $request->attributes->get('id_sai'); //Get the ID of the SAI from the link
$sai = $this->getDoctrine()->getRepository(SAI::class)->find($idSAI); //Find the SAI in the database
$oldUsers = $sai->getUsers(); //Get the users of the SAI
$form = $this->createForm(SAIType::class,$sai); //Create the form, which includes a serial number and a list of users that you can modify, by adding or removing users
$form->handleRequest($request);
if ($form->isSubmitted()) {
$newSAI = $form->getData(); //Get the data of the new SAI
$sai->setSerialNumber($newSAI->getSerialNumber()); //Update the serial number
$usersToAdd = $newSAI->getUsers();
//If the old users are not in the list of users of the SAI returned by the form, it means that they got removed and
//I have to remove them from the current SAI
//(this is where the problem starts, since $oldUsers now contains the same users as the $newSAI, and I don't know why)
//(and of course both of these loops won't work because $oldUsers and $usersToAdd contain the same data)
foreach($oldUsers as $oldUser){
if (!$usersToAdd->contains($oldUser)){
$sai->deleteUser($oldUser);
}
}
//Add the new users to the current SAI
foreach($usersToAdd as $newUser){
$sai->addUser($newUser);
}
//Update SAI
$entityManager->persist($sai);
$entityManager->flush();
return $this->redirectToRoute('sais');
}
return $this->render('management/edit_sai.html.twig', array(
'form' => $form->createView(),
));
}
edit_sai.html.twig
{% extends 'home/index.html.twig' %}
{% block body %}
<div class="main">
<h1>Editar SAI</h1>
<hr>
<div class="formUsers">
{{ form_start(form) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script> $('select[data-select="true"]').select2({}); </script>
{% endblock %}
知道我做错了什么吗?我应该怎么做才能解决它?谢谢
我终于找到问题所在了。
正如 Deniz Aktürk 在评论中所说,使用就足够了:
if ($form->isSubmitted()) {
//Update SAI
$entityManager->persist($sai);
$entityManager->flush();
}
但问题是我的函数名称:
- 在 User.php class 中,我使用了 delteSAI() 而不是 removeSAI()。
- 在 SAI.php class 中,我有 deleteUser() 而不是 removeUser()。
我还在两个 classes 中添加了 cascade={"persist", "remove"}:
User.php
/**
* @ORM\ManyToMany(targetEntity="App\Entity\SAI", inversedBy="users", cascade={"persist", "remove"})
* @ORM\JoinTable(name="users_sais")
*/
private $sais;
SAI.php
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="sais", cascade={"persist", "remove"})
*/
private $users;
我不知道 Symfony4 对函数命名如此严格。我希望这会对某人有所帮助。
我想更新一个名为 SAI 的实体的用户列表,但是在提交表单后,我丢失了我要更新的 SAI 的信息,它被表单返回的 SAI 所取代。
我试图在以下代码中解释评论会发生什么:
EdditSAIController.php
/**
* @Route("/edit_sai/{id}/", name="edit_sai", requirements={"page"="\d+"}))
* @Method({"GET"})
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editSAI(Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$idSAI = $request->attributes->get('id_sai'); //Get the ID of the SAI from the link
$sai = $this->getDoctrine()->getRepository(SAI::class)->find($idSAI); //Find the SAI in the database
$oldUsers = $sai->getUsers(); //Get the users of the SAI
$form = $this->createForm(SAIType::class,$sai); //Create the form, which includes a serial number and a list of users that you can modify, by adding or removing users
$form->handleRequest($request);
if ($form->isSubmitted()) {
$newSAI = $form->getData(); //Get the data of the new SAI
$sai->setSerialNumber($newSAI->getSerialNumber()); //Update the serial number
$usersToAdd = $newSAI->getUsers();
//If the old users are not in the list of users of the SAI returned by the form, it means that they got removed and
//I have to remove them from the current SAI
//(this is where the problem starts, since $oldUsers now contains the same users as the $newSAI, and I don't know why)
//(and of course both of these loops won't work because $oldUsers and $usersToAdd contain the same data)
foreach($oldUsers as $oldUser){
if (!$usersToAdd->contains($oldUser)){
$sai->deleteUser($oldUser);
}
}
//Add the new users to the current SAI
foreach($usersToAdd as $newUser){
$sai->addUser($newUser);
}
//Update SAI
$entityManager->persist($sai);
$entityManager->flush();
return $this->redirectToRoute('sais');
}
return $this->render('management/edit_sai.html.twig', array(
'form' => $form->createView(),
));
}
edit_sai.html.twig
{% extends 'home/index.html.twig' %}
{% block body %}
<div class="main">
<h1>Editar SAI</h1>
<hr>
<div class="formUsers">
{{ form_start(form) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script> $('select[data-select="true"]').select2({}); </script>
{% endblock %}
知道我做错了什么吗?我应该怎么做才能解决它?谢谢
我终于找到问题所在了。
正如 Deniz Aktürk 在评论中所说,使用就足够了:
if ($form->isSubmitted()) {
//Update SAI
$entityManager->persist($sai);
$entityManager->flush();
}
但问题是我的函数名称:
- 在 User.php class 中,我使用了 delteSAI() 而不是 removeSAI()。
- 在 SAI.php class 中,我有 deleteUser() 而不是 removeUser()。
我还在两个 classes 中添加了 cascade={"persist", "remove"}:
User.php
/**
* @ORM\ManyToMany(targetEntity="App\Entity\SAI", inversedBy="users", cascade={"persist", "remove"})
* @ORM\JoinTable(name="users_sais")
*/
private $sais;
SAI.php
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="sais", cascade={"persist", "remove"})
*/
private $users;
我不知道 Symfony4 对函数命名如此严格。我希望这会对某人有所帮助。