Symfony提交按钮不会正确刷新/更新实体

时间:2018-01-22 19:21:43

标签: forms symfony entity submit flush

我的实体"公司"与

有两个ManyToMany关系
  1. 实体"用户"和
  2. entity" Agency"
  3. 相关数据,我希望显示为具有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',
      ));
    

    我的twig模板包括两个下拉列表以及每个实体的两个表格。

          {{ 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>
    

    我也在其他网页上使用这些表格(AssignedUsers和AssignedAgencies),这一切都正常。

    所以现在我的问题: 我希望能够简单地添加和删除用户或代理商。只需取消选择例如来自下拉列表然后提交的用户,它正在工作,用户不再被分配给公司。不知何故,同样的程序对代理商不起作用。我不知道为什么,虽然我在刷新之前和之后丢弃了数据并且它正在倾倒正确的数据,但是当我回到公司时,没有任何改变。 我的控制器动作:

    /**
     * @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(),
      ));
    }
    

    为什么它会冲动用户而不是代理商?

2 个答案:

答案 0 :(得分:1)

我这样解决了我的问题,但可能是@ASOlivieri的回答可能对我有所帮助。 我最初做的是:

在实体机构

  /**
 * @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;

在Entity Corporation

/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Agency", mappedBy="corporations")
**/
private $agencies;

切换时 - >代理商的房地产公司的代码较短,以及关于JoinTable等的代码较长,以及公司所在的房地产代理商。

如果有人能向我解释为什么这是解决方案,我会很开心!

答案 1 :(得分:0)

如果您的实体在其他地方(其他控制器等)工作,那么我怀疑您的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表单选项中设置的那样。