Php 分离实体意味着什么?
在阅读时,我偶然发现了Php 分离实体意味着什么?,php,doctrine-orm,Php,Doctrine Orm,在阅读时,我偶然发现了clear方法 在这里,使用了以下示例: <?php $batchSize = 20; $i = 0; $q = $em->createQuery('select u from MyProject\Model\User u'); $iterableResult = $q->iterate(); foreach ($iterableResult as $row) { $user = $row[0]; $user->increaseCre
clear
方法
在这里,使用了以下示例:
<?php
$batchSize = 20;
$i = 0;
$q = $em->createQuery('select u from MyProject\Model\User u');
$iterableResult = $q->iterate();
foreach ($iterableResult as $row) {
$user = $row[0];
$user->increaseCredit();
$user->calculateNewBonuses();
if (($i % $batchSize) === 0) {
$em->flush(); // Executes all updates.
$em->clear(); // Detaches all objects from Doctrine!
}
++$i;
}
$em->flush();
所以我的问题是:
一个物体脱离教义意味着什么?这意味着什么?使用
clear
有哪些优点和缺点 查看条令参考文档,它看起来像clear()
将有效地释放EntityManager
正在处理的所有对象
所以我的理解是flush()
提交到数据库,而clear()
从内存中释放(至少就条令而言)
查看EntityManager
的clear()
方法和的文档。依次调用UnitOfWork
,因此,对于
UnitOfWork
请查看
仅供参考,从文档(最新2.5版本)中,您可以从EntityManager
获取以下代码:
/**
* Clears the EntityManager. All entities that are currently managed
* by this EntityManager become detached.
*
* @param string $entityName
*/
public function clear($entityName = null)
{
if ($entityName === null) {
$this->unitOfWork->clear();
} else {
//TODO
throw new ORMException("EntityManager#clear(\$entityName) not yet implemented.");
}
}
对于工作单元:
/**
* Clears the UnitOfWork.
*/
public function clear()
{
$this->identityMap =
$this->entityIdentifiers =
$this->originalEntityData =
$this->entityChangeSets =
$this->entityStates =
$this->scheduledForDirtyCheck =
$this->entityInsertions =
$this->entityUpdates =
$this->entityDeletions =
$this->collectionDeletions =
$this->collectionUpdates =
$this->extraUpdates =
$this->readOnlyObjects =
$this->orphanRemovals = array();
if ($this->commitOrderCalculator !== null) {
$this->commitOrderCalculator->clear();
}
if ($this->evm->hasListeners(Events::onClear)) {
$this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em));
}
}
所以当医生说:
清除EntityManager。当前由该EntityManager管理的所有实体都将分离
我的理解是,他们所说的“分离”是指实体经理不再持有这些实体的记录。因此,如果没有其他人提及他们,他们可以将垃圾收集起来。
如果您想确保它确实被垃圾收集并有效地释放内存,那么最好在foreach
进程循环中的某个地方调用
/**
* Clears the UnitOfWork.
*/
public function clear()
{
$this->identityMap =
$this->entityIdentifiers =
$this->originalEntityData =
$this->entityChangeSets =
$this->entityStates =
$this->scheduledForDirtyCheck =
$this->entityInsertions =
$this->entityUpdates =
$this->entityDeletions =
$this->collectionDeletions =
$this->collectionUpdates =
$this->extraUpdates =
$this->readOnlyObjects =
$this->orphanRemovals = array();
if ($this->commitOrderCalculator !== null) {
$this->commitOrderCalculator->clear();
}
if ($this->evm->hasListeners(Events::onClear)) {
$this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em));
}
}