【问题标题】:Symfony2 OneToOne annotation inserting/updating related entitySymfony2 OneToOne 注释插入/更新相关实体
【发布时间】:2014-03-04 07:59:25
【问题描述】:

我有与设置实体相关的产品实体,具有一对一的关系。所有产品均已导入,并且产品在开始时在设置表中没有相关行。

当使用 Symfony 更新产品时,会创建适当的关系,因此我在产品和设置之间有 OneToOne。这是使用简单的 INSERT 语句完成的:

INSERT INTO settings (id_product, setting_1, setting_3, setting_3) VALUES (11153, '0', '1', '1');

由于注释和方法,此插入由 Symfony 自动制作:

$em->merge($product);
$em->flush();

不幸的是,当我下次尝试更新产品时,另一个插入设置表完成,显然我有错误,因为 id_product 是 PK 并且必须是唯一的。如果 Product 和 Setting 之间存在关系,我应该怎么做才能强制 Symfony 执行 UPDATE 而不是 INSERT?我使用最新版本的 Symfony 创作音乐。

【问题讨论】:

  • 您能否发布有关如何更新设置实体的代码?通常, $product->getSetting()->setSomeVar('foobar') 应该更新设置实体而不是创建一个新实体...
  • 我把它贴出来——看看合并方法。当它到达时,它会自动更新(总是新插入 - 问题)由于注释设置实体......
  • 我仍然认为我们需要更多关于注释、级联、使用合并 v 持久等的信息。IME,我只使用合并来管理已与实体管理器分离的实体。如果你使用 persist() 而不是 merge() 会发生什么?

标签: php symfony doctrine-orm entity one-to-one


【解决方案1】:

更新与创建实体的方式不同。这是一个非常简单的示例(您必须处理一些异常等。但代码很“短”以向您展示差异:

1当你创建一个Task时,你会创建一个新的实体Task并将它保存到数据库中

2 更新任务时,首先从数据库中获取该任务,然后将其保存到数据库中。

创建:

public function newAction(Request $request)
    {
        // create a task and give it some dummy data for this example
        $task = new Task();
        $task->setTask('Write a blog post');
        $task->setDueDate(new \DateTime('tomorrow'));

        $form = $this->createFormBuilder($task)
            ->add('task', 'text')
            ->add('dueDate', 'date')
            ->add('save', 'submit')
            ->getForm();

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

            if ($form->isValid()) {

                // perform some action, such as saving the task to the database
                $em = $this->getDoctrine()->getManager();
                $em->persist($task);
                $em->flush();

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

        return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }

更新:

public function updateAction(Request $request, $taskId)
    {
        // get the task
        $task = $this->getDoctrine()
            ->getRepository('AcmeTaskBundle:Task');
            ->find($taskId);

        $form = $this->createFormBuilder($task)
            ->add('task', 'text')
            ->add('dueDate', 'date')
            ->add('save', 'submit')
            ->getForm();

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

            if ($form->isValid()) {

                // update the task
                $em->flush();

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

        return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }

我建议您检查一下:

【讨论】:

  • 今天晚些时候我会测试它。问题是产品实体存在于数据库中并且设置不是在开始......看起来我必须实现一些逻辑来处理这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多