Doctrine does not update a simple array type field(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.
这篇关于Doctrine 不会更新简单的数组类型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 不会更新简单的数组类型字段
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01