DDC-500: Single Table Inheritance Selects #5007

Closed
doctrinebot opened this Issue Apr 7, 2010 · 9 comments

1 participant

@doctrinebot

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.

@doctrinebot

Comment created by mridgway:

Adding another child class just to be clear.

@doctrinebot

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

Comment created by mridgway:

Attached a unit test that may or may not work.

It looks to be a similar issue for sure.

@doctrinebot

Comment created by mridgway:

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

@doctrinebot

Comment created by romanb:

Reproduced successfully and working on it.

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added this to the 2.0-BETA1 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment