Pedro Gabriel
Pedro Gabriel

Reputation: 41

Doctrine 2 Performance Answer

i have a question, i'm using doctrine for 4 years but i dont know if he load all data of a Entity (including relations) when we search the object e.g.:

$entity = $entityManager->find('Entity', $id);

in this case, all relations attributes are setted?

i have a class with much attributes of relation (WITHOUT featch=eager), its cause overload?

Or doctrine knows that only relations is searched when you use a get method of attribute?

if you know a documentation, to clarify, please post a link

Upvotes: 1

Views: 70

Answers (1)

jonathancardoso
jonathancardoso

Reputation: 12716

Lazy loading is the default. The collection data is only retrieved from the database, at the time they are accessed for the first time.

If you want the data to be loaded directly, you need to JOIN the specific collection, and add it to the select clause. Or use the EAGER fetch mode like you mentioned.

From http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#entity-object-graph-traversal

[...] Instead of passing you back a real Author instance and a collection of comments Doctrine will create proxy instances for you. Only if you access these proxies for the first time they will go through the EntityManager and load their state from the database.

Also:

Traversing the object graph for parts that are lazy-loaded will easily trigger lots of SQL queries and will perform badly if used to heavily. Make sure to use DQL to fetch-join all the parts of the object-graph that you need as efficiently as possible.

Upvotes: 1

Related Questions