1. <tfoot id='LMjM9'></tfoot>
    2. <small id='LMjM9'></small><noframes id='LMjM9'>

    3. <legend id='LMjM9'><style id='LMjM9'><dir id='LMjM9'><q id='LMjM9'></q></dir></style></legend>
    4. <i id='LMjM9'><tr id='LMjM9'><dt id='LMjM9'><q id='LMjM9'><span id='LMjM9'><b id='LMjM9'><form id='LMjM9'><ins id='LMjM9'></ins><ul id='LMjM9'></ul><sub id='LMjM9'></sub></form><legend id='LMjM9'></legend><bdo id='LMjM9'><pre id='LMjM9'><center id='LMjM9'></center></pre></bdo></b><th id='LMjM9'></th></span></q></dt></tr></i><div id='LMjM9'><tfoot id='LMjM9'></tfoot><dl id='LMjM9'><fieldset id='LMjM9'></fieldset></dl></div>

        <bdo id='LMjM9'></bdo><ul id='LMjM9'></ul>

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

      Doctrine does not update a simple array type field(Doctrine 不会更新简单的数组类型字段)
      <i id='czATM'><tr id='czATM'><dt id='czATM'><q id='czATM'><span id='czATM'><b id='czATM'><form id='czATM'><ins id='czATM'></ins><ul id='czATM'></ul><sub id='czATM'></sub></form><legend id='czATM'></legend><bdo id='czATM'><pre id='czATM'><center id='czATM'></center></pre></bdo></b><th id='czATM'></th></span></q></dt></tr></i><div id='czATM'><tfoot id='czATM'></tfoot><dl id='czATM'><fieldset id='czATM'></fieldset></dl></div>

            <bdo id='czATM'></bdo><ul id='czATM'></ul>

              <tbody id='czATM'></tbody>
            1. <tfoot id='czATM'></tfoot><legend id='czATM'><style id='czATM'><dir id='czATM'><q id='czATM'></q></dir></style></legend>

              1. <small id='czATM'></small><noframes id='czATM'>

                本文介绍了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 不会更新简单的数组类型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  1. <small id='2pNs9'></small><noframes id='2pNs9'>

                    <i id='2pNs9'><tr id='2pNs9'><dt id='2pNs9'><q id='2pNs9'><span id='2pNs9'><b id='2pNs9'><form id='2pNs9'><ins id='2pNs9'></ins><ul id='2pNs9'></ul><sub id='2pNs9'></sub></form><legend id='2pNs9'></legend><bdo id='2pNs9'><pre id='2pNs9'><center id='2pNs9'></center></pre></bdo></b><th id='2pNs9'></th></span></q></dt></tr></i><div id='2pNs9'><tfoot id='2pNs9'></tfoot><dl id='2pNs9'><fieldset id='2pNs9'></fieldset></dl></div>
                    <tfoot id='2pNs9'></tfoot>

                      <tbody id='2pNs9'></tbody>
                  2. <legend id='2pNs9'><style id='2pNs9'><dir id='2pNs9'><q id='2pNs9'></q></dir></style></legend>

                        • <bdo id='2pNs9'></bdo><ul id='2pNs9'></ul>