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

前端 未结 11 1470
南笙
南笙 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:33

    Here is my solution:

    Context:

    All my entities have id property and getId() method


    Solution:

    $em = $this->getDoctrine()->getManager();
    
    // 1 -> get the proxy object class name
    $proxy_class_name = get_class($proxyObject);
    
    // 2 -> get the real object class name
    $class_name = $em->getClassMetadata($proxy_class_name)->rootEntityName;
    
    // 3 -> get the real object
    $object = $em->find($class_name, $proxyObject->getId());
    

    Problem:

    This solution don't work if id property and getId() method are in a Trait class

    I hope that it can help someone

提交回复
热议问题