在更新时不要更新实体替换它

时间:2016-02-16 03:19:53

标签: symfony symfony2-forms

我有一个具有地址实体的分支实体,一个地址可以链接到许多其他类型的实体,我想要做的是当我在分支实体中编辑地址而不是用新数据编辑地址时我想创建一个新的地址实体并将其附加到分支,现在正在发生的是它编辑地址,问题是我有其他实体链接到该地址。

这是我的表格:

class MBBranchType extends AbstractType{

   protected $em;

   function __construct(EntityManager $em){
       $this->em = $em;
   }

   /**
    * @param FormBuilderInterface $builder
    * @param array $options
    */
   public function buildForm(FormBuilderInterface $builder, array $options)
   {
       $builder
        ->add('branchName')
        ->add('branchCode')
        ->add('destiny', 'entity', array(
            'class' => 'ATRBundle:Destiny',
            'empty_value' => '-- Seleccione --',
            'mapped' => true,
            'query_builder' => function(EntityRepository $er){
                    return $er->createQueryBuilder('d')
                        ->orderBy('d.destinyDesc', 'ASC');
        }))
        ->add('startOperation', 'text')
        ->add('stopOperation', 'text', array('required' => ''))
        ->add('authorizationL1', 'checkbox', array('required' => ''))
        ->add('authorizationL2', 'checkbox', array('required' => ''))
        ->add('authorizationL3', 'checkbox', array('required' => ''))
        ->add('address', new AddressType($this->em, new Address()), array('required' => ''))
        ->add('active', 'checkbox', array('required' => ''));

    $builder->get('startOperation')->addModelTransformer(new StringToDateTransformer());
    $builder->get('stopOperation')->addModelTransformer(new StringToDateTransformer());

   }

   /**
    * @param OptionsResolverInterface $resolver
    */
   public function setDefaultOptions(OptionsResolverInterface $resolver)
   {
       $resolver->setDefaults(array(
           'data_class' => 'MB2\ATRBundle\Entity\MBBranch'
       ));
   }

   /**
    * @return string
   */
   public function getName()
   {
      return 'mb2_atr_mb_branch';
   }
}

这是我的行动

public function editMBBranchAction()
{
    $id = $this->request->query->get('id');
    $entity = $this->em->getRepository('ATRBundle:MBBranch')->find($id);
    $form = $this->createForm(new MBBranchType($this->getDoctrine()->getManager()), $entity);

    $form->handleRequest($this->request);

    if ($form->isValid()) {

        if($entity instanceof MBBranch){

            $addressEntity = $this->em->getRepository('ATRBundle:Address')->findOneBy(

                array('street'=>(string)$entity->getAddress()->getStreet()
                        , 'exteriorNum' => $entity->getAddress()->getExteriorNum();
                        , 'interiorNum' => $entity->getAddress()->getInteriorNum();
                        , 'settlment' => $entity->getAddress()->getSettlment()
                )
            );

            if($addressEntity instanceof \MB2\ATRBundle\Entity\Address){
                $entity->setAddress($addressEntity);
            }else{

                $otherAddress = new Address();

                $otherAddress->setStreet($entity->getAddress()->getStreet());
                $otherAddress->setExteriorNum($entity->getAddress()->getExteriorNum());
                $otherAddress->setInteriorNum($entity->getAddress()->getInteriorNum());
                $otherAddress->setSettlment($entity->getAddress()->getSettlment());

                $entity->setAddress($otherAddress);
            }

            $this->em->flush();
        }

        return $this->redirect($this->generateUrl('admin', array('action' => 'list', 'entity' => $this->entity['name'])));
    }

    return $this->render($this->entity['templates']['edit'], array(
        'form'          => $form->createView(),
        'entity'        => $entity,
    ));
}

正如您所看到的,它检查新的Address实体是否存在,如果它确实存在,则将其设置在$ entity-> setAddress($ addressEntity);,如果它不存在则创建一个新的Address()并且设置值,这确实有效,它创建了一个新的地址并将其保存在表中,但由于某种原因它还更新了旧的地址实体。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

我添加了$ this-> em-> detach($ entity-> getAddress());在附加新地址之前,请参阅示例。

public function editMBBranchAction()
{
    $id = $this->request->query->get('id');
    $entity = $this->em->getRepository('ATRBundle:MBBranch')->find($id);
    $form = $this->createForm(new MBBranchType($this->getDoctrine()->getManager()), $entity);

    $form->handleRequest($this->request);

    if ($form->isValid()) {

        if($entity instanceof MBBranch){

            $this->em->detach($entity->getAddress());

            $addressEntity = $this->em->getRepository('ATRBundle:Address')->findOneBy(

                array('street'=>(string)$entity->getAddress()->getStreet()
                        , 'exteriorNum' => $entity->getAddress()->getExteriorNum();
                        , 'interiorNum' => $entity->getAddress()->getInteriorNum();
                        , 'settlment' => $entity->getAddress()->getSettlment()
                )
            );

            if($addressEntity instanceof \MB2\ATRBundle\Entity\Address){
                $entity->setAddress($addressEntity);
            }else{

                $otherAddress = new Address();

                $otherAddress->setStreet($entity->getAddress()->getStreet());
                $otherAddress->setExteriorNum($entity->getAddress()->getExteriorNum());
                $otherAddress->setInteriorNum($entity->getAddress()->getInteriorNum());
                $otherAddress->setSettlment($entity->getAddress()->getSettlment());

                $entity->setAddress($otherAddress);
            }

            $this->em->flush();
        }

        return $this->redirect($this->generateUrl('admin', array('action' => 'list', 'entity' => $this->entity['name'])));
    }

    return $this->render($this->entity['templates']['edit'], array(
        'form'          => $form->createView(),
        'entity'        => $entity,
    ));
}