Doctrine returns proxy when joining a table with null relationships

I am trying to get objects from one class connected to another class. Not all entities actually combined entities.

This is similar to the following statement:

SELECT a, b FROM A a LEFT JOIN B b ON a.id = b.aid GROUP BY a.id; 

or in code:

 $query_builder = $em->getRepository('repository_of_A')->createQueryBuilder('a'); $query_builder = $query_builder->leftJoin('a.b', b); $query_builder = $query_builder->groupBy('a.id'); $query = $query_builder->select('a, b')->getQuery(); $entities = $query->getResult(); 

Now the problem is that whenever there is no B object for A, Doctrine returns a proxy object for A. Since I work with reflections, I need a real object instead of a proxy.

In the attached screenshot, the object with index 26 does not have the corresponding object B for A (Shop).

Shop Entity (A) is a Proxy

Does anyone know why and how I can solve this problem?

Note. I know that I could just use the class name instead of the object when using reflections, but I would also like to understand the problem here, as this may affect the runtime ...

Edit: Attached screenshot

+5
source share
1 answer

If the problem is that the fields are not loaded, before using reflection check, if Doctrine loaded the object and loaded it differently:

 if ( $object instanceof \Doctrine\Common\Persistence\Proxy && !$object->__isInitialized() ) { $object->__load(); } // ... your code 

But, as I see in the screenshot, you incorrectly identified the problem. If you first select a (as in your example), then there will be no proxy in the list of results.

As I would have guessed, in your example all Entity objects are in some association (not selected by query, but, for example, from $country->getShops(); ), and Shop [70] not a proxy server just because that up to this point Doctrine is already loaded This. If the object is on the map (by identifier) ​​- it is used instead of the proxy server, since it is already loaded.

0
source

Source: https://habr.com/ru/post/1202579/


All Articles