如何确定原则实体是否持续存在? [英] How to determine if a Doctrine entity is persisted?
本文介绍了如何确定原则实体是否持续存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法确定参数是否已被Doctrine持久化的对象?像一个实体管理器方法,检查一个对象不是一个普通的旧对象,但实际上已经在内存/持久化的东西。
Is there a way to determine if a parameter is an object that is already persisted by Doctrine or not? Something like an entity manager method that checks that an object is not a plain old object but actually something already in memory/persisted.
<?php
public function updateStatus(Entity $entity, EntityStatus $entityStatus)
{
$entityManager = $this->getEntityManager();
try {
// checking persisted entity
if (!$entityManager->isPersisted($entity)) {
throw new InvalidArgumentException('Entity is not persisted');
}
// ...
} catch (InvalidArgumentException $e) {
}
}
推荐答案
你必须使用这样的UnitOfWork api:
You have to use UnitOfWork api like this:
$isPersisted = \Doctrine\ORM\UnitOfWork::STATE_MANAGED === $entityManager->getUnitOfWork()->getEntityState($entity);
编辑:正如@Andrew Atkinson所说,似乎 EntityManager->包含($ entity)
是现在的首选方式。
As said by @Andrew Atkinson, it seems EntityManager->contains($entity)
is the preferred way now.
这篇关于如何确定原则实体是否持续存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文