DDC-1600: ORM loads an association without it being ever accessed #2009

Open
opened 2026-01-22 13:37:24 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 15, 2012).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user ambis:

03:14 < beberlei> Ambis, please report a bug on jira with details - this doesnt sound correct

I have a basic "Comment has an author" relation. Because I get each of my users from Redis cache directly using user's ID, the comment tracks both user as relation and also user's id like so:

<field name="userId" column="user_id" type="integer" />
<many-to-one field="user" target-entity="User">
    <join-column name="user_id" referenced-column-name="id" />
</many-to-one>

(both access the same db field)

When I query only comments and access user only by getUserId() which returns the property 'userId' as seen on the mapping, my query logging shows that for each comment author there is a separate query to load it. I do not call getUser() at any point which would access the relation 'user' in the comment. Nothing within the comment entity itself accesses the relation. There seems to be no proxy generated out of the comment entity in my proxies folder.

I've had these problems before but I've always found out what was the reason behind this, but none of those seem to be the case this time. Unfortunately I have too much 2.2 code to roll back to 2.1 and test with that.

Edit:

The query itself is very simple:

CommentRepo->createQueryBuilder('comment')
->where('comment.anotherProperty = :somethingOrOther')
->setParameters(array(...))
->orderBy('comment.id', 'DESC')
->getQuery()
->getResult();

As always, there's lot more stuff to the query, but I've commented it out to that bare minimum when I still get this behavior.

Edit2:

I added to the query
->addSelect(array('comment_user'))
->join('comment.user', 'comment_user')

and of course the extra user queries went away.

I also added a die statement to the getUser() method (yes, above return ;) ) to see that it's really not called and it's not. Also re-checked the comment entity that nothing accesses the ->user property other that constructor and getUser().

Originally created by @doctrinebot on GitHub (Jan 15, 2012). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user ambis: 03:14 < beberlei> Ambis, please report a bug on jira with details - this doesnt sound correct I have a basic "Comment has an author" relation. Because I get each of my users from Redis cache directly using user's ID, the comment tracks both user as relation and also user's id like so: ``` <field name="userId" column="user_id" type="integer" /> <many-to-one field="user" target-entity="User"> <join-column name="user_id" referenced-column-name="id" /> </many-to-one> ``` (both access the same db field) When I query only comments and access user only by getUserId() which returns the property 'userId' as seen on the mapping, my query logging shows that for each comment author there is a separate query to load it. I do not call getUser() at any point which would access the relation 'user' in the comment. Nothing within the comment entity itself accesses the relation. There seems to be no proxy generated out of the comment entity in my proxies folder. I've had these problems before but I've always found out what was the reason behind this, but none of those seem to be the case this time. Unfortunately I have too much 2.2 code to roll back to 2.1 and test with that. Edit: The query itself is very simple: CommentRepo->createQueryBuilder('comment') ->where('comment.anotherProperty = :somethingOrOther') ->setParameters(array(...)) ->orderBy('comment.id', 'DESC') ->getQuery() ->getResult(); As always, there's lot more stuff to the query, but I've commented it out to that bare minimum when I still get this behavior. Edit2: I added to the query ->addSelect(array('comment_user')) ->join('comment.user', 'comment_user') and of course the extra user queries went away. I also added a die statement to the getUser() method (yes, above return ;) ) to see that it's really not called and it's not. Also re-checked the comment entity that nothing accesses the ->user property other that constructor and getUser().
admin added the Bug label 2026-01-22 13:37:24 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2009