I have been searching for the exact mappings for for the DB Schema as shown below
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.
No. You cannot use more than 1 primary key in the table. for that you have composite key which is combination of multiple fields.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With