php - 学说 : Can I flush only one class of entities?

标签 php symfony doctrine-orm

我喜欢在 Symfony2 中将 Doctrine 存储库作为服务传递并避免传递 EntityManager 的一般想法。然而,虽然在读取数据时没问题,但这里的保存逻辑就有点问题了。

我们以此为引用:http://php-and-symfony.matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/ ,但有一个变化,将持久化和刷新分开:

class DoctrineORMCustomerRepository extends EntityRepository implements CustomerRepository
{
    public function persist(Customer $customer)
    {
        $this->_em->persist($customer);
    }

    public function flush()
    {
        $this->_em->flush();
    }
}

问题是您在特定存储库中刷新所有实体中的所有更改。

现在,是否可以只刷新一类实体? (可能级联到依赖实体),所以我基本上可以做类似的事情:

foreach ($customers as $customer) {
    $this->customerRepository->persist($customer);
}

$this->customerRepository->flush();

我考虑过这样的事情:

$this->_em->flush(getUnitOfWork()->getIdentityMap()[$this->_entityName]);

但我一定是误解了什么,因为它不起作用。

编辑:是的,我知道我可以做 $this->_em->flush($entity),但是一个一个地做这个是次优的。我什至知道我可以执行 $this->_em->flush($arrayOfEntities),但要使“foreach”示例以这种方式工作,我必须跟踪存储库复制了一些 Doctrine 内部结构。

最佳答案

试试这个:

$em->flush($entity);

然后 doctrine only 将刷新 $entity,忽略任何其他。

关于php - 学说 : Can I flush only one class of entities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40682082/

相关文章:

php - 从缓存中检索实体时获取相关的 Doctrine2 实体

重写 CSS3 规则以实现跨浏览器兼容性的 PHP 类

php - SilverStripe 中的相关博客文章

symfony - 如何在事件监听器中利用 kernel.terminate

php - 如何在 Symfony 4 中记录登录失败?

validation - 仅在 Symfony2 中验证单个表单字段

doctrine-orm - Symfony2,学说 2 : getResult Object

php - 学说 2 : Can I get a Reference from a Repository instead of from the Entity Manager?

php - 无法在 CentOS 6.4 上使用 NGINX 和 php-fpm 使用 PHP 创建文件

php - 如何显示数据库中与特定用户不是 friend 的用户列表