Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6036849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:59:08+00:00 2026-05-23T05:59:08+00:00

I have an issue with single table inheritance and I’m not sure if I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:59:08+00:00Added an answer on May 23, 2026 at 5:59 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an issue on our page whereby the first time a button posts
I do not currently have this issue , but you never know, and thought
I have a database with a single table right now, which contains the following
Currently in my application I have a single table that is giving me a
I have a multithreaded process which inserts several records into a single table. The
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue using the PEAR libraries on Windows from PHP . Pear
We have an issue related to a Java application running under a (rather old)
I have some issue with a Perl script. It modifies the content of a
I have an issue with using AWK to simply remove a field from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.