2
votes

I'm trying to query an entity and joined an other entity which is a child in an inheritance mapping structure.

The situation is:

  • I have a parent entity (eg. Animal)
  • I have a child entity (eg. Dog)
  • I have an entity with a 1:1 association to the child (Dog House)

I want to query all the dog houses and doctrine to get me their dog owners as well.

The simple DQL query I wrote is:

SELECT o FROM MyBundle:DogHouse h JOIN h.dog d

The error is:

Notice: Undefined index: id in ../vendor/doctrine/lib/Doctrine/ORM/Query/SqlWalker.php line 779, class: ErrorException

Looking at the stack trace it seems that doctrine is trying to join with the id on the child entity instead of the parent

Here is the classes illustrating this case

/**
 *  Animal
 *
 * @ORM\Entity
 * @ORM\Table(name="animal")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="integer")
 * @ORM\DiscriminatorMap({"1"="cat",  "2"="dog"})
 */
class Animal
{
    /**
     * @ORM\Column(name="animal_id", type="integer", nullable=false)
     * @ORM\Id
     */
    protected $id;

    /**
     * @ORM\Column(name="name", type="string", length=200, nullable=false)
     */
    protected $name;

    /* getters, setters... */
}

/**
 * Dog
 *
 * @ORM\Entity
 * @ORM\Table(name="dog")
 */
class Dog extends Animal
{
    /**
     * @ORM\OneToOne(targetEntity="DogHouse", inversedBy="dog")
     */
    private $dogHouse;

    /* getters, setters... */
}


/**
 * Dog House
 * 
 * @ORM\Table(name="dog_house")
 * @ORM\Entity
 */
class DogHouse
{
    /**
     * @ORM\Column(name="dog_house_id", type="integer", nullable=false)
     * @ORM\Id
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Dog", mappedBy="doghouse")
     */
    private $dog;

    /* getters, setters... */
}

Note: This post relates to the exact same problem and wasn't answered

1

1 Answers

0
votes

Your mappings are set up incorrectly...

The one To one relation between Dog / Doghouse should be as follows..

class DogHouse....

 /**
 * @ORM\OneToOne(targetEntity="Dog", mappedBy="doghouse")
 */
private $dog;

Make sure your mappedBy="doghouse"