Proxies with Doctrine
krabouilleur opened this issue · 3 comments
Hi !
When we use Doctrine wich returns proxies, the mapper could not map : it maps property to null.
Is there a solution ?
example dump doctrine result :
App\Entity\Company {#584
-id: 81
-secteur: Proxies\__CG__\App\Entity\Secteur {
-id: 1
-name: null
}
dump($company->getSecteur->getName())
: returns a string
but the mapper found "null"
Hi @krabouilleur, yes this is a problem because Doctrine uses proxies to lazy-load entities. There are a few solutions:
- Add
fetch="EAGER"
to your Doctrine mapping or query, this tells Doctrine to fully load the referenced objects. It is best to only do this on queries where you actually use the proxies, for performance reasons. - You can overwrite the property accessor to always load proxies as is done here
- Add a
beConstructedUsing
where you load the entity, as is suggested here
By far the easiest way of doing this is to tell Doctrine to fetch the entities eagerly. Either on the mapping level if you always need them fully loaded, or on the single query if you only need it for one use case.
Hello, I'm commenting on the issue because I have the updated version of lib and the problem persists. Someone managed to find a solution to the problem.
Hi @jacksonveroneze - sorry, I completely missed your question. Do you still have the same problem? If so, can you give some more context? This so we can make sure it is the same problem as the original question, and to provide a starting point for debugging.