0

I'm new with Symfony2 and Doctrine 2 ORM, then i got a problem with composite FK association.

While the association OneToMany-ManyToOne by a single auto-increment ID works good, i got many troubles with the same kind of association but through a composite PK.

What i would like to do is an entity-structure that mirrors the below shema.

http://imageshack.us/photo/my-images/9/z07m.jpg (Sorry but I cannot insert images into the post)

where

prd_product_data.product_id
prd_product_data.language_id

make up together the PK of 'prd_product_data' and

prd_product_image.pdoduct_id
prd_product_image.language_id

make up the FK linked to the table 'prd_product_data'.

These below

prd_product_image.pdoduct_id
prd_product_image.language_id
prd_product_image.file_image_id

make up, all together, the PK of 'prd_product_image'.

I read that Doctrine 2.1 and up, supports natively the composed PK - FK association but there are very few examples. Moreover, surfing on the web, i found just scattered fragments of similar situations but nothing of useful. The main issue now, is that i'm unable to create one unique object for the two ids, like such an unique id.

For example...

Snapshot for the Entity ProductData where "product and language should make up the same object:

/**
  *
  * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="product")
  */
 protected $products_Pimgs;

 /**
  *
  * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="language")
  */
 protected $products_Limgs;

Snapshot for the Entity ProductImage where *product_id* and *language_id* made up the FK for the entity ProductData:

    /**
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="ProductData", inversedBy="products_Pimgs")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="product_id")
     */
    protected $product;

    /**
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="ProductData", inversedBy="products_Limgs")
     * @ORM\JoinColumn(name="language_id", referencedColumnName="language_id")
     */

    protected $language;

    /**
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="FileImage", inversedBy="files_images")
     * @ORM\JoinColumn(name="file_image_id", referencedColumnName="id")
     */
    protected $file_image;

From these snapshots it's evident that those keys are working separately.

In the end, this is my doctrine version, taken from the composer:

"doctrine/orm": ">=2.2.3,<2.4-dev",

How can i create an unique reference with two objects with ORM?

Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39

1 Answers1

0

Read the documentation chapter Composite Primary Keys.


/**
  * @ORM\Id
  * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="product")
  */
protected $products_Pimgs;

/**
 * @ORM\Id
 * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="language")
 */
protected $products_Limgs;
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130