【问题标题】:Doctrine persist saving in the database without flush学说持久保存在数据库中而不刷新
【发布时间】:2016-03-23 09:24:36
【问题描述】:

我是 Doctrine 的新手,并且遇到了持久化和刷新的问题 即使我不调用刷新,实体也会自动保存到数据库中。 我正在使用带有 Doctrine 2 的 Slim 框架。

     public function addSource($request, $response, $args) {
        $apiData = new \App\Entity\Source();
        $apiData->setSourceName('dasds');
        $apiData->setSourceDesc('jasgd<sg');
        $now = new \DateTime("now");
        $apiData->setCreated($now);
        $apiData->setModified($now);
        $this->sourceResource->entityManager->persist($apiData);
        //$this->entityManager->flush();
        echo 'here';exit;
        return $response->withJSON($apiData);
    }

在上面的代码中,如果我在代码后面加上 die 那么它不会保存到数据库中,但是当我让响应发送回来时,它会自动保存。

【问题讨论】:

  • 有些奇怪:$this->sourceResource->entityManager->persist($apiData); //$this->entityManager->flush(); persist() 和 flush() 应该从同一个 em 执行。

标签: php doctrine-orm doctrine


【解决方案1】:

flush() 正在其他地方调用 - 可能在中间件中,也可能在 index.php 的末尾。

【讨论】:

  • 由于有明确的退出,因此无法在之后调用 flush()
  • @quazardous:没错。使用exit; 不会创建数据库行。没有exit,它是。因此,我得出结论,在addSource() 返回之后会发生flush()……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 2023-03-13
  • 2013-03-01
  • 2011-11-06
相关资源
最近更新 更多