开发者

Doctrine ORM Single Table Inheritance association problem (always Eager loading contrary to documentation))

I have an issue with single table inheritance and I'm not sure if I'm interpreting the documentation correctly.

First: I've not copied my code / entity mappings verbosely (or even using the correct syntax) here as I think the problem can be better communicated abstractly. If this is not acceptable by all means say so and I'll add the full code - but it is LONG and I think my question can be answered without it.

If it helps I can draw an ER diagram to try and communicate what I'm trying to do. If you read the following and think 'hey that should work' - then tell me and I'll upload the real code

Second: I don't use lazy loading anywhere. Before accessing my entities I make sure that I load every related entity that I'm going to be accessing by writing DQL up front - so the following issue is fairly terminal to my application)

The Setup:

I have some entities - these make up the core of my application.

// @entity AnimalTrainingSchool
//   oneToMany: trainingDepartment
     fields: [name / location / year founded]
// @entity trainingDepartment
     oneToMany: animalTrainer
     oneToOne: building
     fields: [capacity]
// @entity animalTrainer
     fields: [name / age / qualification]

I access them frequently and in different contexts - but I commonly iterate though levels and access properties and relations for these entities.

foreach ($animalTrainingSchool as $school){
    echo $school->getName() . ' at ' . $school->getLocation();
    echo 'These are the training departments for this school:';
  开发者_开发知识库  foreach ($school->getTrainingDepartments as $trainingDepartment){
        echo $trainingDepartment->getAnimalTypeTrained() . ' are trained in this department';
    }
}

I make sure that all of these are loaded up front by forming my DQL and executing it - to limit the number of SQL queries (to one). This all works great (fairly standard stuff).

DQL Example : "SELECT ats, td, at FROM AnimalTrainingSchool ats JOIN AnimalTrainingSchool.trainingDepartments td JOIN td.animalTrainer at";

This sets up my collection and means that I can traverse it without having to issue additional queries

The Problem:

I have mapped other entites elsewhere in my application - very similarly to this (NOTE: My overall question is very similar to the below question with one MAJOR difference (see below)

Doctrine 2 Inheritance Mapping with Association

// NB: new awards are issued each time they are awarded - so it is meant to be a oneToOne relationships - the awards are unique
// @entity award
     {id information / table information / discriminator map / inheritance type (SINGLE_TABLE)}
     fields: [medalMaterial / award_serial_number]
//departmentAward extends award
    oneToOne: trainingDepartment
//trainerAward extends award
    oneToOne: animalTrainer

then I made the relationship bidirectional by modifying my initial entities

// @entity trainingDepartment
     oneToMany: animalTrainer
     oneToOne: building
     oneToOne: departmentAward
     fields: [capacity]
// @entity animalTrainer
     fields: [name / age / qualification]
     oneToOne: trainerAward

What Happens

Now when I access my original entities in exactly the same way as above - they automatically (eagerly) load the associated entity for their awards though I'm not telling them to. This is especially bad when I'm iterating though a whole bunch of trainingDepartments / AnimalTrainers and Doctrine is executing an SQL statement for EVERY entity.

For 20 departments with 10 trainers in each - this is 200 additional queries.

//Given the exact same DQL and foreach loop as above (note that at no stage am I accessing awards) - I get a ton of extra queries that look like this

"SELECT award.property1, award.property2, award.property3 FROM awardTable LEFT JOIN trainingDepartmentTable ON award.awardee_id = trainingDepartmentTable.id and award.discriminatorColumn IN ('departmentAward')";
// or...
"SELECT award.property1, award.property2, award.property3 FROM awardTable LEFT JOIN animalTrainerTable ON award.awardee_id = animalTrainerTable.id and award.discriminatorColumn IN ('trainerAward')";

None of what is being generated is incorrect - it's just that having read the following question it seems to me like I have set this up as the documentation describes (and in the opposite way to @Matthieu; namely - that If I related my initial 3 entites to the LOWEST level entities rather than the 'award' base class then they SHOULD be able to use proxies instead of attempting to eagerly load the entities.

Stackoverflow Question which is asking the opposite of what I am describing

Doctrine 2 Inheritance Mapping with Association

Relevant Doctrine Documentation

http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html#performance-impact

There is a general performance consideration with Single Table Inheritance: If you use a STI 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.

It seems to me that regardless of whether or not you are joining to the base level entity or a subclassed entity - Doctrine will eagerly load the associations and will not attempt to use proxies.

Again: I can post real code - but given the length of the question already I felt it was best not to. Any input greatly appreciated.


The inverse side of a oneToOne relationship can not be lazy loaded. In order to support lazy loading, Doctrine needs to create a proxy object, but proxy objects need to have an identifier associated with them. In the case of oneToOne relationships, the identifier is only available on the owning side. So the inverse relationship has to be loaded eagerly.

You should try to fetch join these associations if possible. Version 2.1 will automatically force fetch joins for inverse oneToOne relationships.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜