Symfony 提交按钮没有正确 flush/update 实体
Symfony submit button doesn't flush/update the entity properly
我的实体 "corporation" 与
有两个 ManyToMany 关系
- 实体 "User" 和
- 实体"Agency"
相关数据,我想显示为 table,带有指向 (un-)select 数据的 EntityType 下拉列表。
因此我有以下形式:
$builder
->add('usercorporations', EntityType::class, array(
'class' => 'UserBundle:User',
'query_builder' => function (EntityRepository $er) use ($corporationMarket) {
return $er->createQueryBuilder('e')
->andWhere(':marketIds MEMBER OF e.markets')
->setParameter('marketIds',$corporationMarket)
->orderBy('e.lastName', 'ASC');
},
'choice_label' => function($user) {
return $user->getFirstName() . ' ' . $user->getLastName() ;
},
'expanded' => false, 'multiple' => true,
'required'=>false,
'by_reference' => false,
'empty_value' => "label.select_user",
'translation_domain' => 'Agency'))
->add('agencies', EntityType::class, array(
'class' => 'AppBundle:Agency',
'query_builder' => function (EntityRepository $er){
return $er->createQueryBuilder('a')
->addOrderBy('a.account', 'ASC');
},
'label' => 'label.agencies',
'choice_label' => function($agency) {
return $agency->getId() . ' - ' . $agency->getAccount() . ' - ' . $agency->getCityName() . ', ' . $agency->getState();
},
'expanded' => false, 'multiple' => true,
'required' => false,
'translation_domain' => 'Agency',
));
我的树枝模板包括两个下拉菜单以及每个实体的两个 table。
{{ form_start(form) }}
{# This renders a red banner if the form contains errors.
If the form variable is not called "form", pass it explicitly. #}
{% include 'Form/form_errors_banner.html.twig' %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<legend>{{ 'label.assignedUsers'|trans }}</legend>
{{ form_widget(form.usercorporations) }}
<br>
<br>
{% include 'UserBundle:User:AssignedUsers.html.twig' %}
</div>
</div>
</div>
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<legend>{{ 'label.assignedAgencies'|trans }}</legend>
{{ form_widget(form.agencies) }}
<br>
<br>
{% include 'AppBundle:Agency:AssignedAgencies.html.twig' with {'entity': corporation} %}
</div>
</div>
</div>
<div>
<input type="submit" class="btn-primary btn" value="{{ 'label.apply'|trans }}"/>
</div>
{{ form_end(form) }}
</div>
我也在其他页面上使用 tables(AssignedUsers 和 AssignedAgencies),一切正常。
所以现在我的问题是:
我希望能够简单地添加和删除用户或代理机构。通过 deselecting 例如下拉列表中的用户然后提交,它正在工作,用户不再分配给公司。不知何故,同样的程序对这些机构不起作用。我不知道为什么,我在刷新之前和之后转储了数据并且它正在转储正确的数据,但是当我 return 到公司时,没有任何改变。
我的控制器操作:
/**
* @Route("/corporation/{id}/{page}", name="appBundle_corporation", requirements={"id" = "\d+", "page" = "\d+"}, defaults={"page" = 1}))
*/
public function corporationAction($id, Request $request, $page)
{
$repository = $this->getDoctrine()
->getRepository('AppBundle:Corporation');
$em = $this->getDoctrine ()->getManager ();
$corporation = $repository->findOneById($id);
/*
* CREATE PAGINATION FOR TABLES
*/
//assigned Users
$assignedUsers = $corporation->getUsercorporations();
$assignedAgencies = $corporation->getAgencies();
dump($assignedAgencies);
$form = $this->createForm(CorporationAssignmentType::class,$corporation, array(
'corporationMarket' => $corporationMarket,
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($corporation);
$em->flush();
dump($assignedAgencies);
$this->addFlash(
'success',
'Your changes were saved!'
);
return $this->redirectToRoute('appBundle_corporation', array('id' => $corporation->getId()));
}
return $this->render('AppBundle::corporation.html.twig', array(
'item' => $corporation,
'corporation'=>$corporation,
'assignedUsers'=>$userTable,
'assignedAgencies' => $agencyTable,
'form' => $form->createView(),
));
}
知道为什么它会冲刷用户而不是代理商吗?
如果您的实体在其他地方(其他控制器等)工作,那么我怀疑您必须 by_reference
在您的 agencies
表单选项中设置为 false。
->add('agencies', EntityType::class, array(
'class' => 'AppBundle:Agency',
'query_builder' => function (EntityRepository $er){
return $er->createQueryBuilder('a')
->addOrderBy('a.account', 'ASC');
},
'label' => 'label.agencies',
'choice_label' => function($agency) {
return $agency->getId() . ' - ' . $agency->getAccount() . ' - ' . $agency->getCityName() . ', ' . $agency->getState();
},
'expanded' => false, 'multiple' => true,
'required' => false,
'by_reference' => false,
'translation_domain' => 'Agency',
));
您的 user
表单选项中似乎是这样设置的。
我用这种方法解决了我的问题,但@ASOlivieri 的回答可能对 es 很有帮助。
我最初做的是:
在实体机构
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Corporation", inversedBy="agencies", cascade={"persist"})
* @ORM\JoinTable(name="app_agency_corporations",
* joinColumns={@ORM\JoinColumn(name="agency_id", referencedColumnName="iata8")},
* inverseJoinColumns={@ORM\JoinColumn(name="corporation_id", referencedColumnName="ucid")})
* @var \AppBundle\Entity\Corporation
**/
private $corporations;
实体公司
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Agency", mappedBy="corporations")
**/
private $agencies;
切换时 --> 较短的代码指向该机构的 属性 个公司,而有关 JoinTable 等的较长代码则适用于该机构的 属性 个代理机构。
如果有人能向我解释为什么这是解决方案,我会很高兴!
我的实体 "corporation" 与
有两个 ManyToMany 关系- 实体 "User" 和
- 实体"Agency"
相关数据,我想显示为 table,带有指向 (un-)select 数据的 EntityType 下拉列表。 因此我有以下形式:
$builder
->add('usercorporations', EntityType::class, array(
'class' => 'UserBundle:User',
'query_builder' => function (EntityRepository $er) use ($corporationMarket) {
return $er->createQueryBuilder('e')
->andWhere(':marketIds MEMBER OF e.markets')
->setParameter('marketIds',$corporationMarket)
->orderBy('e.lastName', 'ASC');
},
'choice_label' => function($user) {
return $user->getFirstName() . ' ' . $user->getLastName() ;
},
'expanded' => false, 'multiple' => true,
'required'=>false,
'by_reference' => false,
'empty_value' => "label.select_user",
'translation_domain' => 'Agency'))
->add('agencies', EntityType::class, array(
'class' => 'AppBundle:Agency',
'query_builder' => function (EntityRepository $er){
return $er->createQueryBuilder('a')
->addOrderBy('a.account', 'ASC');
},
'label' => 'label.agencies',
'choice_label' => function($agency) {
return $agency->getId() . ' - ' . $agency->getAccount() . ' - ' . $agency->getCityName() . ', ' . $agency->getState();
},
'expanded' => false, 'multiple' => true,
'required' => false,
'translation_domain' => 'Agency',
));
我的树枝模板包括两个下拉菜单以及每个实体的两个 table。
{{ form_start(form) }}
{# This renders a red banner if the form contains errors.
If the form variable is not called "form", pass it explicitly. #}
{% include 'Form/form_errors_banner.html.twig' %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<legend>{{ 'label.assignedUsers'|trans }}</legend>
{{ form_widget(form.usercorporations) }}
<br>
<br>
{% include 'UserBundle:User:AssignedUsers.html.twig' %}
</div>
</div>
</div>
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<legend>{{ 'label.assignedAgencies'|trans }}</legend>
{{ form_widget(form.agencies) }}
<br>
<br>
{% include 'AppBundle:Agency:AssignedAgencies.html.twig' with {'entity': corporation} %}
</div>
</div>
</div>
<div>
<input type="submit" class="btn-primary btn" value="{{ 'label.apply'|trans }}"/>
</div>
{{ form_end(form) }}
</div>
我也在其他页面上使用 tables(AssignedUsers 和 AssignedAgencies),一切正常。
所以现在我的问题是: 我希望能够简单地添加和删除用户或代理机构。通过 deselecting 例如下拉列表中的用户然后提交,它正在工作,用户不再分配给公司。不知何故,同样的程序对这些机构不起作用。我不知道为什么,我在刷新之前和之后转储了数据并且它正在转储正确的数据,但是当我 return 到公司时,没有任何改变。 我的控制器操作:
/**
* @Route("/corporation/{id}/{page}", name="appBundle_corporation", requirements={"id" = "\d+", "page" = "\d+"}, defaults={"page" = 1}))
*/
public function corporationAction($id, Request $request, $page)
{
$repository = $this->getDoctrine()
->getRepository('AppBundle:Corporation');
$em = $this->getDoctrine ()->getManager ();
$corporation = $repository->findOneById($id);
/*
* CREATE PAGINATION FOR TABLES
*/
//assigned Users
$assignedUsers = $corporation->getUsercorporations();
$assignedAgencies = $corporation->getAgencies();
dump($assignedAgencies);
$form = $this->createForm(CorporationAssignmentType::class,$corporation, array(
'corporationMarket' => $corporationMarket,
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($corporation);
$em->flush();
dump($assignedAgencies);
$this->addFlash(
'success',
'Your changes were saved!'
);
return $this->redirectToRoute('appBundle_corporation', array('id' => $corporation->getId()));
}
return $this->render('AppBundle::corporation.html.twig', array(
'item' => $corporation,
'corporation'=>$corporation,
'assignedUsers'=>$userTable,
'assignedAgencies' => $agencyTable,
'form' => $form->createView(),
));
}
知道为什么它会冲刷用户而不是代理商吗?
如果您的实体在其他地方(其他控制器等)工作,那么我怀疑您必须 by_reference
在您的 agencies
表单选项中设置为 false。
->add('agencies', EntityType::class, array(
'class' => 'AppBundle:Agency',
'query_builder' => function (EntityRepository $er){
return $er->createQueryBuilder('a')
->addOrderBy('a.account', 'ASC');
},
'label' => 'label.agencies',
'choice_label' => function($agency) {
return $agency->getId() . ' - ' . $agency->getAccount() . ' - ' . $agency->getCityName() . ', ' . $agency->getState();
},
'expanded' => false, 'multiple' => true,
'required' => false,
'by_reference' => false,
'translation_domain' => 'Agency',
));
您的 user
表单选项中似乎是这样设置的。
我用这种方法解决了我的问题,但@ASOlivieri 的回答可能对 es 很有帮助。 我最初做的是:
在实体机构
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Corporation", inversedBy="agencies", cascade={"persist"})
* @ORM\JoinTable(name="app_agency_corporations",
* joinColumns={@ORM\JoinColumn(name="agency_id", referencedColumnName="iata8")},
* inverseJoinColumns={@ORM\JoinColumn(name="corporation_id", referencedColumnName="ucid")})
* @var \AppBundle\Entity\Corporation
**/
private $corporations;
实体公司
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Agency", mappedBy="corporations")
**/
private $agencies;
切换时 --> 较短的代码指向该机构的 属性 个公司,而有关 JoinTable 等的较长代码则适用于该机构的 属性 个代理机构。
如果有人能向我解释为什么这是解决方案,我会很高兴!