[DDC-500] Single Table Inheritance Selects Created: 07/Apr/10  Updated: 26/Apr/10  Resolved: 26/Apr/10

Status: Closed
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.0-BETA1
Fix Version/s: 2.0-BETA1
Security Level: All

Type: Bug Priority: Critical
Reporter: Michael Ridgway Assignee: Roman S. Borschel
Resolution: Fixed Votes: 0
Labels: None

Attachments: File DDC500Test.php    
Issue Links:
Reference
is referenced by DDC-497 find() and findAll() on Repository do... Closed

 Description   

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.



 Comments   
Comment by Michael Ridgway [ 07/Apr/10 ]

Adding another child class just to be clear.

Comment by Roman S. Borschel [ 07/Apr/10 ]

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.

Comment by Michael Ridgway [ 07/Apr/10 ]

Attached a unit test that may or may not work.

It looks to be a similar issue for sure.

Comment by Michael Ridgway [ 08/Apr/10 ]

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

Comment by Roman S. Borschel [ 19/Apr/10 ]

Reproduced successfully and working on it.

Comment by Roman S. Borschel [ 26/Apr/10 ]

Fixed in http://github.com/doctrine/doctrine2/commit/760ea34a0cc3cae4e3caea17e8aab6ceb74ecace

Generated at Thu Jul 31 17:35:18 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.