Doctrine 不会更新简单的数组类型字段

可以存储数组而不是映射关联.在 Symfony2 中,使用 collection Field Type 相当容易.例如,使用此技术,您可以存储填充数组事件字段的文本字段数组.但是,要更新数组,有一个技巧,@Vadim Ashikhman 在接受的答案中很好地解释了这个技巧.

It is possible to store an array instead of a mapped association. In Symfony2, this is fairly easy using the collection Field Type. For example, using this technique, you could store an array of text fields that populate an array events field. However, to update an array, there is a trick, and this trick is beautifully explained by @Vadim Ashikhman in the accepted answer.

有时存储数组而不是映射关联更有用且更有效.但是,一旦创建,如果这个数组的大小没有改变,更新这个数组仍然很复杂?

Sometimes it is useful and more efficient to store an array instead of a mapped association. However, once created, it remains complicated to update this Array if the size of that array does not change?

很多人都有类似问题,但是没有人找到解决这个问题的合适方法.

Many people have a similar issue but nobody found a proper solution to this problem.

一个团队可以组织许多活动.这些事件使用 Doctrine 而不是使用 OneToMany 关联简单地存储在一个数组中.因此,实体Event没有与Doctrine映射.

A team can organise many events. These events are simply stored within an array using Doctrine instead of using a OneToMany association. Therefore, the entity Event is not mapped with Doctrine.

<?php

namespace AcmeTestBundleEntity;

...

class Event
{

    /**
     * @AssertNotBlank
     */
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }


}

实体团队

<?php

namespace AcmeTestBundleEntity;

...

/**
 * @ORMEntity()
 * @ORMHasLifecycleCallbacks
 * @ORMTable(name="teams")  
 */
class Team 
{

/**
     * @ORMColumn(type="array")
     * @var array
     */
    protected $events;


    public function addEvent($event)
    {
        if (!in_array($event, $this->events, true)) {
            $this->events[] = $event;
        }

        return $this;
    }

    public function removeEvent($event)
    {
        if (false !== $key = array_search($event, $this->events, true)) {
            unset($this->events[$key]);
            $this->events = array_values($this->events);
        }

        return $this;
    }

    public function getEvents()
    {
        return $this->events;
    }

    public function setEvents(array $events)
    {
        $this->events = array();

        foreach ($events as $event) {
            $this->addEvent($event);
        }

        return $this;
    }

活动形式

<?php
namespace AcmeTestBundleFormType;

...

class EventType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('name', 'text');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AcmeTestBundleEntityEvent',
            'cascade_validation' => true,
        ));
    }

    ...
}

团队形式

<?php

namespace AcmeTestBundleFormType;

...

class TeamType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('events','collection', array(
            'type' => new EventType(),
            'allow_add'   => true,
            'allow_delete' => true,
            'prototype' => true,
            'by_reference' => false,
            'options' => array('data_class' => 'AcmeTestBundleEntityEvent'),
            )
        );

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AcmeTestBundleEntityTeam',
        ));
    }
    ...

}

控制器

/**
 * Update a team
 *
 * @Route("update/{team_id}", name="updateTeamFromId")
 * @Template("AcmeTestBundle:Team:teamUpdate.html.twig")
 */
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()){

            $em->persist($team_to_update);
            $em->flush();

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

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

}

推荐答案

你可以在控制器里面试试这个:

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.

相关文章