mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Fetch joins and EntityManager::find() returns proxy #5223
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @mreiche on GitHub (Aug 19, 2016).
Originally assigned to: @kimhemsoe on GitHub.
Hi. Im querying entites as a fetch join in DQL
SELECT d,s FROM domains d JOIN d.site sd.site is defined as
@ManyToOne(targetEntity="Site", inversedBy="assignedDomains", fetch="LAZY")But one of these entities is returned as a proxy when I log the output of the result.
log:
As you can see, that even when I try to load the entity with EntityManager::find() I still get a proxy object of this entity.
Im using Doctrine 2.5.4
@kimhemsoe commented on GitHub (Aug 19, 2016):
I am guessing that the Site entity with id 46 has its proxy created by relation from another previous loaded entity.
Try to look after entities with relations to Site which could have been loaded.
@mreiche commented on GitHub (Aug 19, 2016):
@kimhemsoe The Site entity 46 is definitively already loaded, it is the current site im working on.
@kimhemsoe commented on GitHub (Aug 19, 2016):
Then you have your answer why it is a proxy.
When you load $domain it will create proxy for site 46. That is how doctrine works. If you want to avoid that. You will need to fetch site 46 when loading the domain. You can this with dql.
@mreiche commented on GitHub (Aug 19, 2016):
It seems that I misunterstand the concept of fetch joins.
In reference to the documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#joins
A fetch join is exactly what Im doing with DQL:
I expect no Proxies in this result. What I'm doing wrong here? Is there a difference between
$query->execute()and$query->getResult()in this case?@kimhemsoe commented on GitHub (Aug 19, 2016):
That is the correct way to do a fetch join.
If you still see the proxy, it is because that that entity already had been created. It is not possible to "replace" an object reference and there can only be one instance of each entity. So even if you load the data for site 46, it does not magical convert it to an none proxy.
There is nothing wrong proxies and you should not try to work against them. They are there to make your job easier. So i would look pass them and figure different ways to archive your goals.
ex.
Log::info('nav', 'loaded: ' . $site->getClassName() .': '.$site2->getId());
Or something like that.
execute() executes and getResult() returns result and executes if needed. There is difference in using either.
@mreiche commented on GitHub (Aug 19, 2016):
Got that. Ok I'm looking for a workaround by myself. Thanks.