Php 缓存在同一请求中且未更新的托管实体
我的代码相当复杂,因此我将尝试以最简单的方式进行解释 我有一个父实体Php 缓存在同一请求中且未更新的托管实体,php,doctrine-orm,zend-framework2,doctrine-odm,Php,Doctrine Orm,Zend Framework2,Doctrine Odm,我的代码相当复杂,因此我将尝试以最简单的方式进行解释 我有一个父实体ValueList。此“列表”有许多ValueListItems class ValueList { //... /** * @ODM\ReferenceMany( * targetDocument="JobboardBase\Entity\ValueListItem", * sort={"order"="asc"}, * cascade={"all"} * ) */
ValueList
。此“列表”有许多ValueListItems
class ValueList
{
//...
/**
* @ODM\ReferenceMany(
* targetDocument="JobboardBase\Entity\ValueListItem",
* sort={"order"="asc"},
* cascade={"all"}
* )
*/
protected $items;
}
然后,我有了一个服务方法,它将一个新的ValueListItem
添加到此(已管理的)ValueList
public function createValueListItem(ValueListItem $item, ValueList $list)
{
try {
$om = $this->getObjectManager();
$om->persist($item);
$list->addItem($item);
$om->persist($list);
$om->flush();
return $item;
} catch (\Exception $e) {
throw $e;
}
}
这会将实体正确添加到Mongo集合中。但是,因为我正在使用AJAX调用执行控制器操作,所以我还需要重新分派“indexAction”以异步返回更新的“list”HTML
// ListItemController::createAndAttachValueItemToParentListAction()
// ....
// Below is the successful 'add' of the above method call return
if ($service->createValueListItem($form->getData(), $list)) {
$content = $this->forward()->dispatch('JobboardBase\Controller\ListItem', array(
'action' => 'index',
'id' => $list->getId()
));
return $this->jsonModel(array(
'success' => true,
'messages' => array($message),
'content' => $content
));
//... IndexAction
public function indexAction() {
// ...
$items = $list->getItems(); // Returns 0 (when there should be 1)
//...
}
通过forward()
调用(在$content
中)返回的HTML不包括新添加的ValueListItem
实体。但是,当我刷新页面时,它将正确显示
条令似乎返回一个缓存的值列表
实体,该实体不包括新添加的值列表项
——只有在发出新请求时,才会显示新项
我的问题是,为什么学说返回的是“旧”实体而不是更新的实体?我的印象是,它应该是同一个实例,因此通过引用进行更新 您可以使用实体管理器
refresh
方法使用实际数据刷新模型:
$om->refresh($list);
你有没有试过刷新($list)?@claustrofob Dam,没有!从未在文档中看到过
refresh()
!现在搜索一点,它似乎就是我想要的-谢谢(添加答案;-))