Esteban
Esteban

Reputation: 89

Best practices on Doctrine and Symfony

I would like to know what it is preferable for the optimisation between 2 approachs in ORM.

I wonder it especially in the case of hotel booking and the availbility of some rooms. This kind of search includes a lot of different entities with complex queries. Just thinking about the querybuilder makes me an headache... But i'm not sure that's the best way to populate all entites in php and then apply my functions....

Upvotes: 0

Views: 1484

Answers (2)

Pete Mitchell
Pete Mitchell

Reputation: 2879

Generally you should be getting the objects you require via queries built using the query builder. You should not get all your entities via ->findAll() and then filtering with php as this could be very resource intensive - imagine you had 1,000,000 entries in your database?

If you were looking at hotel bookings and room availability - you would write a query that only returns objects that have room availability - rather than checking all objects with php.

I wrote a blog post on some best practices with doctrine which can be found here - http://www.uvd.co.uk/blog/some-doctrine-2-best-practices/

Upvotes: 1

BlackCharly
BlackCharly

Reputation: 649

I think it's allways better to create an QueryBuilder (or simply write a DQL query) to get your results.

When you get all your objects with a getDoctrine()->findAll() call, a PHP object is created and his values are set for every result. Then your PHP script (which is certainly slower than the database) will only keep interesting objects, you will have a lot of garbadge.

When you use a QueryBuilder, the database optimizes your query's processing and returns only the interesting objects. You avoid a lots of object instanciation and don't need to filter results with PHP.

Upvotes: 0

Related Questions