DDC-500: Single Table Inheritance Selects #625

Closed
opened 2026-01-22 12:44:58 +01:00 by admin · 9 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 7, 2010).

Jira issue originally created by user mridgway:

We have a set of models that use Single Table inheritance and we are trying to select all objects of one type:

/****
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="type", type="string")
 * @DiscriminatorMap({"Child"="Child", "OtherChild"="OtherChild"})
*/
abstract class ParentModel
{
    /****
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /*** @Column(type="string") **/
    public $property;

    function getId() {return $this->id;}
}

abstract class SubParent extends ParentModel
{
}

/****
 * @Entity
 */
class Child extends SubParent
{
    /*** @Column(type="string") **/
    public $anotherProperty;
}

/****
 * @Entity
 */
class OtherChild extends SubParent
{
    /*** @Column(type="string") **/
    public $someOtherProperty;
}

Now to query for all of the Child objects we do:

$children = $em->getRepository('Child')->findall();
foreach($children AS $child) {
    echo $child->getId();
}

but we get "Notice: Undefined index: id in ./Doctrine/ORM/UnitOfWork.php on line 1727" on the finAll() call and the objects that aren't of the correct type have their properties nulled out. This is because the query that is being executed doesn't have any conditionals for the type of model it's looking for and the ORM doesn't check to make sure that the object is of the correct type.

This could be solved by having conditionals in the SQL query (chaining a bunch of 'or' statements for all of the child objects) or by pulling back all of the objects and then filtering out what isn't of the correct type. Unfortunately neither solution seems ideal.

I'll try to make a test case for this then.

Originally created by @doctrinebot on GitHub (Apr 7, 2010). Jira issue originally created by user mridgway: We have a set of models that use Single Table inheritance and we are trying to select all objects of one type: ``` /**** * @Entity * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="type", type="string") * @DiscriminatorMap({"Child"="Child", "OtherChild"="OtherChild"}) */ abstract class ParentModel { /**** * @Id @Column(name="id", type="integer") * @GeneratedValue(strategy="AUTO") */ protected $id; /*** @Column(type="string") **/ public $property; function getId() {return $this->id;} } abstract class SubParent extends ParentModel { } /**** * @Entity */ class Child extends SubParent { /*** @Column(type="string") **/ public $anotherProperty; } /**** * @Entity */ class OtherChild extends SubParent { /*** @Column(type="string") **/ public $someOtherProperty; } ``` Now to query for all of the Child objects we do: ``` $children = $em->getRepository('Child')->findall(); foreach($children AS $child) { echo $child->getId(); } ``` but we get "Notice: Undefined index: id in ./Doctrine/ORM/UnitOfWork.php on line 1727" on the finAll() call and the objects that aren't of the correct type have their properties nulled out. This is because the query that is being executed doesn't have any conditionals for the type of model it's looking for and the ORM doesn't check to make sure that the object is of the correct type. This could be solved by having conditionals in the SQL query (chaining a bunch of 'or' statements for all of the child objects) or by pulling back all of the objects and then filtering out what isn't of the correct type. Unfortunately neither solution seems ideal. I'll try to make a test case for this then.
admin added the Bug label 2026-01-22 12:44:58 +01:00
admin closed this issue 2026-01-22 12:44:59 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 7, 2010):

@doctrinebot commented on GitHub (Apr 7, 2010): - is referenced by [DDC-497: find() and findAll() on Repository do not work when SINGLE_TABLE inheritance is used](http://www.doctrine-project.org/jira/browse/DDC-497)
Author
Owner

@doctrinebot commented on GitHub (Apr 7, 2010):

Comment created by mridgway:

Adding another child class just to be clear.

@doctrinebot commented on GitHub (Apr 7, 2010): Comment created by mridgway: Adding another child class just to be clear.
Author
Owner

@doctrinebot commented on GitHub (Apr 7, 2010):

Comment created by romanb:

Related to DDC-497 ? Of course there should be conditionals in the query when querying for a subtype. It really surprises me that this seems not to be the case. Maybe there has been some regression.

@doctrinebot commented on GitHub (Apr 7, 2010): Comment created by romanb: Related to [DDC-497](http://www.doctrine-project.org/jira/browse/DDC-497) ? Of course there should be conditionals in the query when querying for a subtype. It really surprises me that this seems not to be the case. Maybe there has been some regression.
Author
Owner

@doctrinebot commented on GitHub (Apr 7, 2010):

Comment created by mridgway:

Attached a unit test that may or may not work.

It looks to be a similar issue for sure.

@doctrinebot commented on GitHub (Apr 7, 2010): Comment created by mridgway: Attached a unit test that may or may not work. It looks to be a similar issue for sure.
Author
Owner

@doctrinebot commented on GitHub (Apr 8, 2010):

Comment created by mridgway:

Fixed test case. Now gives the 'Undefined index: id' error.

@doctrinebot commented on GitHub (Apr 8, 2010): Comment created by mridgway: Fixed test case. Now gives the 'Undefined index: id' error.
Author
Owner

@doctrinebot commented on GitHub (Apr 19, 2010):

Comment created by romanb:

Reproduced successfully and working on it.

@doctrinebot commented on GitHub (Apr 19, 2010): Comment created by romanb: Reproduced successfully and working on it.
Author
Owner

@doctrinebot commented on GitHub (Apr 26, 2010):

Comment created by romanb:

Fixed in 760ea34a0c

@doctrinebot commented on GitHub (Apr 26, 2010): Comment created by romanb: Fixed in http://github.com/doctrine/doctrine2/commit/760ea34a0cc3cae4e3caea17e8aab6ceb74ecace
Author
Owner

@doctrinebot commented on GitHub (Apr 26, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Apr 26, 2010): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/f9bf6e2d281a60efd056

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/f9bf6e2d281a60efd056 - [10561_DDC500Test.php](https://gist.github.com/f9bf6e2d281a60efd056#file-10561_DDC500Test-php)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#625