据我所知,像property_exists()这样的反射方法不能在doctrine2代理对象上工作。
在这种情况下,通过关系$user->getCity()检索代理
在这种情况下,如何检查属性是否存在/是否已设置?
发布于 2016-11-08 23:46:58
解决方案是ReflectionClass::getParentClass()。
所以像这样的代码应该可以工作:
$reflect = new \ReflectionClass($proxyObject);
if ($proxyObject instanceof \Doctrine\Common\Persistence\Proxy)
// This gets the real object, the one that the Proxy extends
$reflect = $reflect->getParentClass();
$privateProperty = $reflect->getProperty('privateProperty');
$privateProperty->setAccessible(true);
$privateProperty->setValue($proxyObject, $yourNewValue);发布于 2013-02-14 09:35:09
您可能需要先检查代理是否已初始化:
if (
$entity instanceof \Doctrine\Common\Persistence\Proxy
&& ! $entity->__isInitialized()
) {
$proxy->__load();
}这基本上强制加载代理:在此之后,一切都将像您拥有原始实体的实例一样工作。
顺便说一句,ORM目前不支持公共属性,尽管该功能将在Doctrine ORM 2.4中实现。这样,您就可以访问公共属性,而不必担心对象是否是代理。
https://stackoverflow.com/questions/14812123
复制相似问题