Php Symfony/条令未在内存中更新
我怀疑这是一个关于实体不在内存中更新的简单问题 以下是我的存储库功能:Php Symfony/条令未在内存中更新,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我怀疑这是一个关于实体不在内存中更新的简单问题 以下是我的存储库功能: public function insertTask($information) { ... $entityManager = $this->getEntityManager(); $entityManager->persist($taskObj); $entityManager->flush(); // Update th
public function insertTask($information)
{
...
$entityManager = $this->getEntityManager();
$entityManager->persist($taskObj);
$entityManager->flush();
// Update the task defects for this task
$this->updateTaskDefectTaskIds($taskObj->getId(), $task['defects']);
return $taskObj;
}
private function updateTaskDefectTaskIds($task, $defects)
{
$taskDefectIds = array();
foreach ($defects as $defect)
{
$taskDefectIds[] = intval($defect['taskDefectId']);
}
// Update the task IDs on the task defects
$this->getEntityManager()
->createQuery('UPDATE Model:TaskDefect td SET td.task = :task WHERE td.id IN (:taskDefectIds)')
->setParameter('task', $task)
->setParameter('taskDefectIds', $taskDefectIds)
->execute();
$this->getEntityManager()->flush();
}
这似乎是可行的。基本上,我有:
-任务表
-任务缺陷表
TaskDefects由于应用程序的编写方式,在任务执行之前插入。但这意味着,一旦任务最终添加,我需要更新TaskDefect记录以指向新的TaskId
不过,我面临的问题是,内存中的某个地方(从它正确更新的实际数据库方面来看)没有接收到我的更改。例如,我更新了一些TaskDefects以指向新的TaskId,但随后我访问了Task对象,它说没有缺陷
如果我转到另一个页面,尝试访问同一个任务,它就会说有缺陷
所以我觉得我缺少了一个flush()
或persist()
或阻止内存中实体更新的东西。显然,重新加载一个页面会强制刷新,然后就可以正常工作了
以下是我控制器中的内容:
$task = $repository->insertTask($content); // i figured maybe at this point it's too much to expect the task obj to magically update
$updatedTask = $repository->findOneById($task->getId()); // so I grab it again... but no luck
var_dump($updatedTask->getDefects()); // ... because this returns no defects
欢迎任何意见。谢谢。插入任务中的这一行:
return $taskObj;
与UpdateTaskDefectTaskId中发生的事情没有任何联系,因为您根本没有修改$taskObj,您只是传递一个id值,然后通过DQL更新缺陷对象
如果希望$taskObj反映insertTask中添加的缺陷,可以执行以下操作:
public function insertTask($information)
{
...
$entityManager = $this->getEntityManager();
$entityManager->persist($taskObj);
$entityManager->flush();
// Update the task defects for this task
$this->updateTaskDefectTaskIds($taskObj, $task['defects']);
return $taskObj;
}
private function updateTaskDefectTaskIds($taskObj, $defects)
{
foreach ($defects as $defect)
{
$defect = $this->getEntityManager()->getRepository('YourBundle:Defect')->find(intval($defect['taskDefectId']));
if ($defect instanceof Defect) {
$defect->setTaskObj($taskObj);
$this->getEntityManager()->persist($defect);
$taskObj->addDefect($defect);
}
}
$this->getEntityManager()->persist($taskObj);
$this->getEntityManager()->flush();
}
$this->getEntityManager()->refresh($taskObj);
return $taskObj;
或者,如果您不介意额外的db调用,只需在insertTask中刷新$taskObj,如下所示:
public function insertTask($information)
{
...
$entityManager = $this->getEntityManager();
$entityManager->persist($taskObj);
$entityManager->flush();
// Update the task defects for this task
$this->updateTaskDefectTaskIds($taskObj, $task['defects']);
return $taskObj;
}
private function updateTaskDefectTaskIds($taskObj, $defects)
{
foreach ($defects as $defect)
{
$defect = $this->getEntityManager()->getRepository('YourBundle:Defect')->find(intval($defect['taskDefectId']));
if ($defect instanceof Defect) {
$defect->setTaskObj($taskObj);
$this->getEntityManager()->persist($defect);
$taskObj->addDefect($defect);
}
}
$this->getEntityManager()->persist($taskObj);
$this->getEntityManager()->flush();
}
$this->getEntityManager()->refresh($taskObj);
return $taskObj;
此外,doctrine喜欢缓存内存中的内容,因此如果它没有任何理由检查db(在您的代码示例中,它无法知道您的更改),那么当您通过id获取实体时,它会很高兴地为您提供过时的对象。非常感谢您的回复。您非常正确,并且
$this->getEntityManager()->刷新($taskObj)代码>是修复它所需要的全部。干杯