-1

I've recently started rebuilding a old PHP application using Silex, Doctrine and Twig.

One of the main entities is "issue" (It's a issue tracking system), the issue has relations to many other entities, like "status", "Owner", "creator" , etc. I've created all entities as doctrime ORM entities and defined all relationships.

From the controller, I fetch one "issue" and send it to the twig template like this:

$issue = $app['orm.em']->getRepository('MMW\Entity\Issue')->find($issueId);

In the twig template i can easily print out the related entities, like:

{{ issue.status.name }}

and

{{ issue.owner.name }}

It works like a charm, but when I enable logging to see what queries are send to the database like this:

$em->getConnection()
  ->getConfiguration()
  ->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());

I notice that for each join, a separate query is send to the database. I would think that performance wise, one query with SQL joins would be much faster.

Should I build my application in a different way, maybe use the querybuilder in the controller to fetch the exact columns and entities I need? Or is there a way to force doctrine to build the query in a different way?

Or should I just not worry about it and leave it as is...?

ErikL
  • 2,031
  • 6
  • 34
  • 57
  • 1
    How is this a problem related to `twig`? You need to hydrate your `JOIN`'s with doctrine thats all... – DarkBee Dec 11 '17 at 07:38
  • could you explain how that would be done then? Is that something I should do in the entities? or something I can do with the find I execute? – ErikL Dec 11 '17 at 07:53
  • 2
    https://stackoverflow.com/questions/26891658/what-is-the-difference-between-fetch-eager-and-fetch-lazy-in-doctrine – DarkBee Dec 11 '17 at 08:08
  • ok, interesting, so I can user fetch="EAGER" on the relation to for instance always make sure the creator is loaded.It seems that doctrine does however assume inner joins, while for instance the "owner" is not always present. Wouldn't building the query with the query builder not still be the better option, so that I can specify which joins should be inner joins and which should be left joins? – ErikL Dec 11 '17 at 10:45
  • 1
    Yes, that is true. This is discussed [here](https://tideways.io/profiler/blog/5-doctrine-orm-performance-traps-you-should-avoid) at point `5`as well. It all depends on the data you are working with anyhow – DarkBee Dec 11 '17 at 11:07

1 Answers1

0

You should use Doctrine fetch joins to fetch the entire entity and prevent this way to access for every field. I recommend you to read the Doctrine documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#joins

Isaac Bosca
  • 1,588
  • 1
  • 15
  • 34