Doctrine does not update a simple array type field

巧了我就是萌 提交于 2019-12-03 02:16:50
Vadim Ashikhman

You can find the answer here:

How to force Doctrine to update array type fields?

Refs:

Doctrine uses identical operator === to compare changes between old and new values. The operator used on the same object with different data always return true. There is the other way to solve this issue, you can clone an object that needs to be changed.

$items = $myEntityObject->getItems(); 
$items[0] = clone $items[0];
$items[0]->setSomething(123); 
$myEntityObject->setItems($items);

Or change the setItems() method

public function setItems($items) 
{
    if (!empty($items) && $items === $this->items) {
        reset($items);
        $key = key($items);
        $items[$key] = clone $items[$key];
    }
    $this->items = $items; 
}

You can try this inside the controller:

public function updateTeamAction($team_id, Request $request)
{

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

    $repository= $em->getRepository('AcmeTestBundle:Team');

    $team_to_update = $repository->find($team_id);

    $form = $this->createForm(new teamType(), $team_to_update);

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

        if ($form->isValid()){
            $events = $team_to_update->getEvents();
            foreach($events as $key => $value){
                $team_to_update->removeEvent($key);
            }
            $em->flush();
            $team_to_update->setEvents($events);
            $em->persist($team_to_update);
            $em->flush();

            return $this->redirect($this->generateUrl('homepage'))  ;
        }
    }

    return array(
    'form' => $form->createView(),
    'team_id' => $team_id,
    );

}

There is probably a beter way to do this and i know this isnt a nice way to do it but till you (or someone else) finds that solution you can use this as a temporary fix.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!