dtp87205 2018-05-20 14:40
浏览 8

Symfony - >在更新时覆盖现有实体

Im creating a simple user class
- User -> entity
- Address -> entity

When a user got created i create a address record in my db.

Now when a user gets edited and changed his addres :
If the address exist he need to take that existing address
if the address not exist Symfony needs to create a new one and use the new address.

Atm I can create users and their address,
but when I edit a users address:
The current existing adress get overwrited instead of creating a new one.

Maybe i missed some logic or am i doing it wrong.

here's my code :

public function editAction(Request $request,Users $user)
{

    $form = $this->createForm(UserForm::class, $user);

    if ($request->getMethod() == 'POST') {

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $form = $form->getData();

            $this->tryEditAction($user, $form);

            return $this->redirectToRoute('admin_userslist');
        }
    }


    $label = "Gebruiker aanpassen";
    return $this->render('@ProjectUser/backend/edit.html.twig', array(
        'form' => $form->createView(),
        'user' => $user,
        'label' => $label,

    ));
}

And here is the TryEditAction :

public function tryEditAction($user, $form)
{

    $formAddress = $form->getAddress();

    $formAddressStreet = $formAddress->getStreet();
    $formAddressStreetNr = $formAddress->getStreetNr();
    $formAddressStreetBus = $formAddress->getStreetBus();


    $entityManager = $this->getDoctrine()->getManager();


        //***
        // Getting the entitymanager to get all addresses stored in our db
        // Check if this address exist, so we change or edit the address accordingly to the user
        //**

    $address = $entityManager->getRepository('ProjectLocationBundle:Address')
        ->findOneBy(
            array(
                'street' => $formAddressStreet,
                'street_nr' => $formAddressStreetNr,
                'street_bus' => $formAddressStreetBus
            )

        );
    if($address):

        $foundAddress = $address;
        $user->setAddress($foundAddress);
        $foundAddress->setUser($user);
        $entityManager->persist($foundAddress);
    else:
        $newAddress = new Address();
        $newAddress = $formAddress;
        $entityManager->persist($newAddress);
        $entityManager->flush($newAddress);
        $user->setAddress($newAddress);

    endif;


   // dump($user); die;

    $userName = $user->getUsername();
    $slug = $this->slugify($userName);
    $user->setSlug($slug);

    $entityManager->persist($user);
    $entityManager->flush();

    return $user;


}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等