dongwei9771 2013-12-19 11:42
浏览 22
已采纳

托管实体缓存在同一请求中而不进行更新

My code is fairly complex so I will try to explain in the simplest way possible

I have a parent entity ValueList. This 'list' has many ValueListItems.

class ValueList
{
  //...
  /**
   * @ODM\ReferenceMany(
   *   targetDocument="JobboardBase\Entity\ValueListItem", 
   *   sort={"order"="asc"}, 
   *   cascade={"all"}
   * )
   */
   protected $items;
}

I then have a service method that adds a new ValueListItem to this (already managed) 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;
  }
}

This adds the entity correctly to the Mongo collection. However because I am executing the controller action with an AJAX call I also need to re-dispatch the 'indexAction' to return a the updated 'list' HTML asynchronously.

// 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)
  //...
}

The HTML returned via the forward() call (in $content) doesn't include the new added ValueListItem entity. It will however display correctly when I refresh the page.

Doctrine seems to be returning a cached ValueList entity that doesn't include the newly added ValueListItem - Only when a new requested is made does the new item get displayed.

My question is why is doctrine returning the 'old' entity rather than the updated entity? I was under the impression that it should be the same instance and therefore updated by reference?

  • 写回答

1条回答 默认 最新

  • doufunuo4787 2013-12-19 15:43
    关注

    you can refresh your model with the actual data using entity manager refresh method:

    $om->refresh($list);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题