isom
isom

Reputation: 304

Doctrine 2 Composite Primary Keys as Foreign Key

I have ClassA mapped to the entity ClassB with ManyToOne relation (simple so far).

class ClassA{
    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(name="keyA", type="string", length=255)
     */
    private $keyA;


    /**
     * @var ClassB $classB
     * @ORM\ManyToOne(targetEntity="ClassB", inversedBy="classAs")
     * @ORM\JoinColumns({
     *  @ORM\JoinColumn(name="ClassB_keyB", referencedColumnName="keyB")
     * })
     */
    private $classB;
}

And this is the ClassB:

class ClassB{
    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(name="keyB", type="string", length=255)
     */
 private $keyB;

    /**
     *
     * @var ClassC $classC
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="ClassC", inversedBy="classBs")
     * @ORM\JoinColumns({
     *  @ORM\JoinColumn(name="ClassC_keyC", referencedColumnName="keyC")
     * })
     */
    private $classC;

    /**
     * @var ArrayCollection $classAs
     * @ORM\OneToMany(targetEntity="ClassA", mappedBy="classB")
     */
    private $classAs;
}

As you can notice the ClassB contains a composite primary key (2 entities and on column).
And this is the ClassC:

class ClassC{
    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(name="keyC", type="string", length=255)
     */
    private $keyC;


    /**
     * @var ArrayCollection $classBs
     * @ORM\OneToMany(targetEntity="ClassB", mappedBy="classC")
     */
    private $classBs;
}

Whene i try to display all entities of ClassA (using findAll()) I get this exception Missing value for primary key classC on ERP\................\ClassB

What am I missing here ?!

Upvotes: 1

Views: 1382

Answers (1)

walidtlili
walidtlili

Reputation: 1100

you have to create your own method at ClassARepository and add your join columns

Upvotes: 1

Related Questions