Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Regular vs Fetch join

Tags:

php

doctrine

in doctrine, whats the diff between regular and fetch join? i dont get it just by reading the docs.

// regular
$query = $em->createQuery("SELECT u FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();

// fetch
$query = $em->createQuery("SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();

whats the purpose of fetch join? if i select u, a why am i just getting users ($users = $query->getResult();)? if i use a regular join, i can probably use $user->getAddresses() to access the related objects?

like image 269
Jiew Meng Avatar asked Jan 22 '23 17:01

Jiew Meng


1 Answers

I think the docs describe it pretty well. If you use a fetch join, the related entities will be included in the hydrated result. Otherwise they won't, and if you then try to access them it'll fire off another query to get the information.

It's simply a matter of whether or not it should be included in the results.

like image 112
Daniel Egeberg Avatar answered Jan 24 '23 06:01

Daniel Egeberg