I have an entity, let's call it Foo
and a second one Bar
Foo
can (but doesn't have to) have one or multiple Bar
entries assigned. It looks something like this:
/**
* @ORM\OneToMany(targetEntity="Bar", mappedBy="foo")
* @ORM\OrderBy({"name" = "ASC"})
*/
private $bars;
I now would like to load in one case only Foo
entities that have at least one Bar
entity assigned. Previously, there was one foreach
loop to traverse all Foo
entries and if it had assigned entries, the Foo
entry got assigned to an array.
My current implementation is in the FooRepository
a function called findIfTheresBar
which looks like this:
$qb = $this->_em->createQueryBuilder()
->select('e')
->from($this->_entityName, 'e')
/* some where stuff here */
->addOrderBy('e.name', 'ASC')
->join('e.bars', 'b')
->groupBy('e.id');
Is this the correct way to load such entries? Is there a better (faster) way? It kind of feels as if it should have a having(...)
in the query.
EDIT:
I've investigated it a little further. The query should return 373 out of 437 entries.
- Version 1: only using
join()
, this loaded 373 entries in 7.88ms - Version 2: using
join()
andhaving()
, this loaded 373 entries in 8.91ms - Version 3: only using
leftJoin()
, this loaded all 437 entries (which isn't desired) in 8.05ms - Version 4: using
leftJoin()
andhaving()
, this loaded 373 entries in 8.14ms
Since Version 1 which only uses an innerJoin
as @Chausser pointed out, is the fastest, I will stick to that one.
Note: I'm not saying Version 1 will be the fastest in all scenarios and on every hardware, so kind of a follow up question, does anybody know about a performance comparison?