Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Composite Primary Keys mapping Doctrine2

Tags:

doctrine-orm

I have been searching for the exact mappings for for the DB Schema as shown belowSample Database Schema

As one can see the product_i18n table has two composite foreign keys (product_id and locale_id).

Now once the product and locale entities are finished, I wanted to insert the data (name and description) into the product_i18n table.

Is there an example which is similar to this type of mapping using Doctrine 2. Or in case if some one can give a brief overview how to approach this type of mapping, then your information is appreciated.

P.S. In case if one requires more information regarding this, then please dont hesitate to ask.

like image 508
125369 Avatar asked Dec 11 '12 09:12

125369


People also ask

Can a table have multiple composite keys?

No. You cannot use more than 1 primary key in the table. for that you have composite key which is combination of multiple fields.

How can I map a composite primary key in hibernate?

A composite primary key is mapped using an Embeddable type in hibernate. We'll first create an Embeddable type called EmployeeIdentity containing the employeeId and companyId fields, and then create the Employee entity which will embed the EmployeeIdentity type.

Can you have 3 primary keys?

A table can only ever have a one primary key. It is not possible to create a table with two different primary keys. You can create a table with two different unique indexes (which are much like a primary key) but only one primary key can exist.


1 Answers

Doctrine 2 supports composite keys natively

/**
 * @Entity
 */
class ProductI18N
{
    /** @Id @Column(type="string") */
    private $product;
    /** @Id @Column(type="string") */
    private $locale

    public function __construct($product, $locale)
    {
        $this->product= $product;
        $this->locale= $locale;
    }

Always keep in mind that the composite key MUST be setted before persist the model:

$productI18N = new ProductI18N("Guanabana", "CR");
$em->persist($productI18N );
$em->flush();

For more information about you can see the documentation: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html

like image 165
manix Avatar answered Jan 02 '23 21:01

manix