<small id='YIk7V'></small><noframes id='YIk7V'>

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

    1. <legend id='YIk7V'><style id='YIk7V'><dir id='YIk7V'><q id='YIk7V'></q></dir></style></legend>

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

      1. Symfony 2 UniqueEntity repositoryMethod 在更新实体时失败

        Symfony 2 UniqueEntity repositoryMethod fails on Update Entity(Symfony 2 UniqueEntity repositoryMethod 在更新实体时失败)
          <tfoot id='b1gFL'></tfoot>
            <tbody id='b1gFL'></tbody>

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

                  <legend id='b1gFL'><style id='b1gFL'><dir id='b1gFL'><q id='b1gFL'></q></dir></style></legend>

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

                  本文介绍了Symfony 2 UniqueEntity repositoryMethod 在更新实体时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一些简单的脚本,但无法解决这个问题.所以就是这里了.

                  I'm working on some simple script, but can't wrap my head around this problem. So here it is.

                  /**
                   * Booking
                   * @ORMTable()
                   * @ORMEntity(repositoryClass="TonsBookingBundleEntityBookingRepository")
                   * @UniqueEntity(fields={"room", "since", "till"}, repositoryMethod="getInterferingRoomBookings")
                   * @AssertCallback(methods={"isSinceLessThanTill"}, groups={"search","default"})
                   */
                  class Booking
                  

                  和存储库方法

                  /**
                       * Get room state for a certain time period
                       *
                       * @param array $criteria
                       * @return array
                       */
                      public function getInterferingRoomBookings(array $criteria)
                      {
                          /** @var $room Room */
                          $room = $criteria['room'];
                          $builder = $this->getQueryForRoomsBooked($criteria);
                          $builder->andWhere("ira.room = :room")->setParameter("room", $room);
                          return $builder->getQuery()->getArrayResult();
                      }
                  

                  问题是这适用于它应该的创建方法,但是当更新现有实体时 - 它违反了这个限制.

                  The problem is that this works on create methods like it should, but when updating existing entity - it violates this constrain.

                  我尝试添加Id约束,但是在创建实体时,id为null,因此repository Method甚至没有启动.我也尝试删除实体,然后重新创建它.喜欢

                  I tried to add Id constrain, but when creating entities, id is null, so repository Method don't even starts. Also i tried to remove Entity and then recreate it. like

                  $em->remove($entity);
                  $em->flush();
                  //-----------
                  $em->persist($entity);
                  $em->flush();
                  

                  但这也不起作用.

                  创建动作

                   /**
                       * Creates a new Booking entity.
                       *
                       * @Route("/create", name="booking_create")
                       * @Method("POST")
                       * @Template("TonsBookingBundle:Booking:new.html.twig")
                       */
                      public function createAction(Request $request)
                      {
                          $entity = new Booking();
                          $form = $this->createForm(new BookingType(), $entity);
                          $form->bind($request);
                          if ($form->isValid())
                          {
                              $em = $this->getDoctrine()->getManager();
                              $room = $entity->getRoom();
                              if($room->getLocked() && $room->getLockedBy()->getId() === $this->getUser()->getId())
                              {
                                  $entity->setCreatedAt(new DateTime());
                                  $entity->setUpdatedAt(new DateTime());
                                  $entity->setManager($this->getUser());
                  
                                  $em->persist($entity);
                                  $room->setLocked(false);
                                  $room->setLockedBy(null);
                                  $room->setLockedAt(null);
                                  $em->persist($room);
                                  $em->flush();
                                  return $this->redirect($this->generateUrl('booking_show', array('id' => $entity->getId())));
                              }
                              else
                              {
                                  $form->addError(new FormError("Номер в текущий момент не заблокирован или заблокирован другим менеджером"));
                                  return array(
                                      'entity' => $entity,
                                      'form' => $form->createView(),
                                  );
                              }
                          }
                  
                          return array(
                              'entity' => $entity,
                              'form' => $form->createView(),
                          );
                      }
                  

                  更新操作

                   /**
                       * Edits an existing Booking entity.
                       *
                       * @Route("/edit/{id}/save", name="booking_update")
                       * @Method("PUT")
                       * @Template("TonsBookingBundle:Booking:edit.html.twig")
                       */
                      public function updateAction(Request $request, $id)
                      {
                          /** @var $em EntityManager */
                          $em = $this->getDoctrine()->getManager();
                  
                          $entity = $em->getRepository('TonsBookingBundle:Booking')->find($id);
                  
                          if (!$entity) {
                              throw $this->createNotFoundException('Unable to find Booking entity.');
                          }
                  
                          $editForm = $this->createForm(new BookingType(), $entity);
                          $editForm->bind($request);
                  
                          if ($editForm->isValid()) {
                              $em->persist($entity);
                              $em->flush();
                  
                              return $this->redirect($this->generateUrl('booking_edit', array('id' => $id)));
                          }
                  
                          return array(
                              'entity' => $entity,
                              'form' => $editForm->createView(),
                          );
                      }
                  

                  推荐答案

                  我明白了!我把注释改成了这个

                  I got this! I changed annotation to this

                  /**
                   * Booking
                   * @ORMTable()
                   * @ORMEntity(repositoryClass="TonsBookingBundleEntityBookingRepository")
                   * @UniqueEntity(fields={"id","room", "since", "till"}, repositoryMethod="getInterferingRoomBookings")
                   * @UniqueEntity(fields={"room", "since", "till"}, repositoryMethod="getInterferingRoomBookings",groups={"create"})
                   * @AssertCallback(methods={"isSinceLessThanTill"}, groups={"search","default"})
                   */
                  class Booking
                  

                  将 BookingType 复制到 BookingTypeCreate 并添加

                  Copy BookingType to BookingTypeCreate And added

                   public function setDefaultOptions(OptionsResolverInterface $resolver)
                      {
                          $resolver->setDefaults(array(
                              'data_class' => 'TonsBookingBundleEntityBooking',
                              'validation_groups' => array('create'),
                          ));
                      }
                  

                  形成默认值.所以现在将实体传递给验证方法时参数是不同的.我认为这仍然是一种解决方法.

                  To form defaults. So now parameters are different when passing entity to validation method. I think it's still a workaround method.

                  这篇关于Symfony 2 UniqueEntity repositoryMethod 在更新实体时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

                      <legend id='cZSAA'><style id='cZSAA'><dir id='cZSAA'><q id='cZSAA'></q></dir></style></legend>
                        <tbody id='cZSAA'></tbody>

                      <small id='cZSAA'></small><noframes id='cZSAA'>

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

                            <bdo id='cZSAA'></bdo><ul id='cZSAA'></ul>
                            <tfoot id='cZSAA'></tfoot>