【问题标题】:Doctrine 2 update data into Database but not update data into EntityDoctrine 2 将数据更新到数据库但不将数据更新到实体
【发布时间】:2014-06-08 21:06:07
【问题描述】:

使用学说 2 在 zend 框架 2 上工作。

在进行单元测试时,我使用“测试数据库”来添加、编辑和删除值。

问题是我添加了一个新实体,好的。 添加新实体后,女巫工作完美,我尝试更新实体。

//set all values
$this->getEntityManager->persist($dataEntity);
$this->getEntityManager->flush()

当我尝试使用以下方法检索更新的数据时:

    $produtoAcessorioCompareEntity = $this->getEntityManager()->getRepository('Administrador\Entity\ProdutoAcessorio')->findOneBy(array(
        'idProdutoAcessorio' => $produtoAcessorioEntity->getIdProdutoAcessorio()
    ));

但这缓存了来自 ADD 方法的旧数据,并没有更新新实体 $produtoAcessorioCompareEntity 的数据,我得到了旧值。

当我检查数据库时,新值就在那里。所以应该有一个技巧来重新组织我可能丢失的加载实体。

如何在 Persist 和 Flush 之后更新工作实体,以通过 $this->getEntityManager->getRepository() 获取新值而不是打开新连接?

【问题讨论】:

  • $entityManager->clear();

标签: php unit-testing symfony doctrine-orm


【解决方案1】:

$this->getEntityManager()->刷新($entity);

【讨论】:

  • 我遇到了完全相同的问题,这成功了。谢谢!
【解决方案2】:

好吧,您的代码似乎没有错,但是,您可以尝试使用find() 方法:

$produtoAcessorioCompareEntity = $this->getEntityManager()
    ->find('Administrador\Entit\ProdutoAcessorio', $produtoAcessorioEntity->getIdProdutoAcessorio());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2020-09-22
    相关资源
    最近更新 更多