Reynier
Reynier

Reputation: 2478

How do I generate ID when I have composite keys in Doctrine?

I have a Entity with composite keys. See below:

class BankAccount {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * 
     */
    protected $bank;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="CompanyBundle\Entity\Company")
     */
    protected $company;

    ...

}

because Doctrine has some issues with composite keys it won't generates sequences (I'm working in PostgreSQL), how do I deal with this in order to generate $bank which is the PK?

Upvotes: 0

Views: 144

Answers (1)

mattexx
mattexx

Reputation: 6606

If sounds like you don't want a composite key, just a primary key on $bank and a foreign key on $company. If that's the case,

class BankAccount {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $bank;

    /**
     * @ORM\ManyToOne(targetEntity="CompanyBundle\Entity\Company")
     */
    protected $company;

    ...

}

should do it.

Upvotes: 1

Related Questions