Doctrine2 - proxy object among regular objects in getResult

In my Symfony2 controller, I have two requests:
as in this example:

$object = $this->getDoctrine()->getManager()
           ->createQuery('SELECT PARTIAL o.{id,name,field1} 
                          FROM SomeBundle:SomeEntity o  
                          WHERE o.id = :objectId')
                    ->setParameter('objectId', $objectId)
                    ->getResult();



$objects = $this->getDoctrine()->getManager()
           ->createQuery('SELECT PARTIAL o.{id,name,field1, field2} 
                          FROM SomeBundle:SomeEntity o ')
                    ->getResult();

The effect that I get in the collection $objectsis a collection of objects SomeBundle:SomeEntity, except for the one I got the variable $objectfor which I get the proxy object.

If I output the collection $objects, and for each element I want to print the output, which includes the fields: name, field1, field2, I get null for field2 for this object. In fact, if I get this $objectin any other controller running along with this, field2 is also zero for every object reference.

For example, if I want to display each object as:

name field1 field2

I get the following array for $objects:

nameExample field1Example field2Example
nameExample field1Example field2Example
nameExample field1Example 
nameExample field1Example field2Example
nameExample field1Example field2Example

- $object.
2 , getResult . , Entity Request.

  • ?
  • ? ( HYDRATE_ARRAY)
  • ?

.

+4
1

, Doctrine . , , . , , . . http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork.html#how-doctrine-keeps-track-of-objects

- , ( $em->detach($object) $em->clear()), . , , , .

- $em->refresh($object) ( ), Doctrine, :

$query = $this->getDoctrine()->getManager()->createQuery('SELECT PARTIAL o.{id,name,field1, field2} FROM SomeBundle:SomeEntity o ');
$query->setHint(Query::HINT_REFRESH, true);
$objects = $query->getResult();

Doctrine , .

+2

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


All Articles