Getting a “true” object from a proxy object in doctrine2

前端 未结 11 1467
南笙
南笙 2021-02-01 13:50

Doctrine uses proxy objects to represent related objects in order to facilitate lazy loading. This is a really cool feature, but its causing an issue with something I am trying

11条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 14:41

    @mustaccio, @Heather Orr

    I had this problem. Thanks for idea,

    This is the code:

    // $object is a Proxy class
    $objectClass = get_class($object);
    $reflectionClass = new \ReflectionClass($objectClass);
    
    // Make sure we are not using a Proxy class
    if ($obj instanceof Proxy) {
        $reflClass = $reflClass->getParentClass();
    }
    

    Another way, if you just need to read annotations:

    // $object is a Proxy class
    $objectClass = get_class($object);
    $classMetadata = $this->em->getClassMetadata($objectClass);
    
    foreach ($classMetadata->getReflectionProperties() as $property) {
        $annotation = $this->annotationReader->getPropertyAnnotation($property, YOUR_ANNOTATION::class)
    }
    

    Not absolutely related to original question, but I hope this will help some people...

提交回复
热议问题