关于doctrine2代理对象的思考

Tho*_*s K 3 php reflection symfony doctrine-orm

据我所知,反射方法property_exists()在doctrine2代理对象上不起作用。

在这种情况下,将通过关系检索代理 $user->getCity()

在这种情况下,如何检查属性是否存在/已设置?

Aer*_*dir 5

解决方法是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);
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。谢谢@aerendir (2认同)