Doctrine 2 - 无法访问新创建的记录(在 Doctrine 之外)

Doctrine 2 - Unable to access newly created record (outside of Doctrine)

关于我的环境的一些重要说明:

情况是:

// 1. Create a user via **WP** function (which returns ID)
$wp_user_id = wp_insert_user($wp_user_array);

// 2. Then, I need to immediately retrieve that user object via Doctrine
$wp_user = $MyDb->em->getRepository('WpUsers')->findOneBy(array('id'=>$wp_user_id));

// 3. RESULT = NULL

没有找到对象,大概是因为Entity被Doctrine缓存或存储在内存的某处。

如何强制 Doctrine 查看数据库并获取这个新创建的用户?

您可以使用

清除教义缓存
EntityManager::clear()

参见http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html(搜索清除方法)

这个问题的解决方案非常简单。在尝试 clear()refresh() 并强制清除 Doctrine 的缓存后,我 运行 别无选择。

事实证明我必须做些什么才能让 Doctrine 检查新记录是否已创建、关闭并打开连接:

$this->_em->getConnection()->close();
$this->_em->getConnection()->connect();

我知道这不是最理想的解决方案,但它确实有效而且我已经调试了五天了!