I have an entity Parent
with oneToMany
relation to Child
which is related to User
.
Child
collection in Parent
is indexed with user_id
, which means, it utilizes indexBy
mapping option.
Entity\Parent:
type: entity
oneToMany:
children:
targetEntity: Entity\Child
mappedBy: parent
indexBy: user_id
cascade: [ persist ]
Now, in Parent
I'd like a method which would tell me if there is a Child
for particular User
in its collection. To do that I have following code in Parent
:
class Parent {
public function hasChild(User $user) {
return isset($this->children[$user->getId()]);
}
}
This works as expected.
But there's a performance issue with this approach. When I access Parent::children
, Doctrine loads whole collection, which may be a few thousands or Child
instances.
It there any way to to check that without loading whole collection, but keeping the current interface? I want it to be done via Parent
class, not Child
repository etc.