gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:08:30 29 4
gpt4 key购买 nike

我喜欢在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com