NOTE : if what I want is not possible, a "not possible" answer will be accepted
In the Doctrine 2 documentation about inheritance mapping, it says there are 2 ways :
- Single table inheritance (STI)
- Class table inheritance (CTI)
For both, there is the warning :
If you use a STI/CTI entity as a many-to-one or one-to-one entity you should never use one of the classes at the upper levels of the inheritance hierachy as “targetEntity”, only those that have no subclasses. Otherwise Doctrine CANNOT create proxy instances of this entity and will ALWAYS load the entity eagerly.
So, how can I proceed to use inheritance with an association to the base (abstract) class ? (and keep the performance of course)
Example
A user has many Pet
(abstract class extended by Dog
or Cat
).
What I want to do :
class User {
/**
* @var array(Pet) (array of Dog or Cat)
*/
private $pets;
}
Because of the warning in Doctrine documentation, I should do that :
class User {
/**
* @var array(Dog)
*/
private $dogs;
/**
* @var array(Cat)
*/
private $cats;
}
This is annoying, because I loose the benefits of inheritance !
Note : I didn't add the Doctrine annotations for the mapping to DB, but you can understand what I mean
I think you've misunderstood, the section of the manual you've quoted is entitled "Performance impact", they're not telling you you can't do this, only that there are performance implications if you do. This makes sense for lazy loading - for heterogeneous collections of STI entities you have to go to the database and load the entity before you know what class it will be, so lazy loading isn't possible / doesn't make sense. I'm learning Doctrine 2 myself at the moment, so I mocked up your example, the following works OK for more:
... and the test script ....
All my cats and dogs load as the correct type:
If you look at the generated SQL, you'll notice that when the OneToMany targetEntity is "pet", you get SQL like this:
But when it's set to Cat, you get this:
HTH.
I'm tired, but this seems like much ado about nothing.
You missed the important bit of that warning:
That's not the case in your example! If you had not omitted the doctrine annotations, you might have noticed.
The association User::pets is OneToMany, not [One|Many]ToOne. One user has many pets.
The inverse association is OneToOne, but it's targeting User, which has no inheritance.
Robin's answer should have been a good hint -- you can log the sql queries and see what doctrine actually does to your database!
The bad-for-performance scenario is something like:
Now, if you wanted to iterate over all the blue collars, Doctrine runs into some trouble. It doesn't know what class $owner is going to be, so it can't use a Proxy. Instead, it's forced to eagerly load $owner to find out whether it's a Cat or a Dog.
This isn't a problem for OneToMany or ManyToMany relationships, because in that case, lazy loading works fine. Instead of a proxy, you get a PersistentCollection. And a PersistentCollection is always just a PersistentCollection. It doesn't care about it's own contents until you actually ask for them. So lazy loading works fine.