Question

I have three entities which are "Addressable" (can have an Address):

Delivery Points
Customers
Distribution Centers

Every Addressable entity also is Geolocatable (has latitude and longitude), but i have entities that are Geolocatable but not Addressable, like Track Points and Route Points (an ordered sequence of lat and lngs).

What is the best way (please take performance in account, as some tables may have millions of rows), to achieve this with doctrine 2 (using Symfony2 and YAML mapping)?

My question is: what is the best approach for this problem? mapped supperclasses? single table inheritance? multi table inheritance? I didn't get which one is best for this by reading doctrine's docs.

I'm using PostgreSQL, if that matters.

Was it helpful?

Solution 2

The fastest option would be not using any table-inheritance at all and mapping all entities to distinct tables but if you want to use table-inheritance for some reason ...

single table inheritance is faster by architecture as there are less joins in the queries ... see this article to get an idea of how table inheritance works/looks ...

metadata is usually cached so there will not be a noticable performance impact between using mapped-superclasses or providing complete mappings (which would be bad in terms DRY) for every single of your entities.

The real performance gain will come from optimizing your queries and caching these and their results.

OTHER TIPS

This is definitely a situation where composition is a better fit than inheritance.

The language you use to describe the problem is instructive. You define "Addressable" as "can have and Address, which is different than "A Customer is a type of address". In the latter case, inheritance would make more sense.

Level zero is just use associations (Address OneToOne Geolocation, Customer OneToOne Address, RoutePoint OneToOne Geolocation), etc).

If you want to perform a set of operations on anything that has an Address, create an interface called Addressable, and have your Customer implement it.

If you're running PHP 5.4, you can use traits to DRY up implementing those interfaces.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top