Basic idea: I have 4 Doctrine entities: Product, Shop, ProductOccurence and Price. A product occurs in one or more shops and prices are tied to both products and shops.
So for instance laptop A (a Product
) is sold by both shop A and shop B (so there are 2 ProductOccurence
instances in the DB, linking to the same Product
, but different Shop
entities) and has a history of 3 list prices in shop A and 4 prices in shop B (so there are 7 instances in the table for entity Price
).
When I doctrine:schema:create, I get this error:
[Doctrine\ORM\ORMException]
Column name `product_id` referenced for relation from TRP\ModelBundle\Entity\Price towards TRP\ModelBundle\Entity\ProductOccurence does not exist.
Removing the annotation for Price::$occurence
(the one with the multiple join columns) solves the issue, but I want to understand why it shows up in the first place. Thanks!
Here's part of Product.php
/**
* Product
*
* @ORM\Table(name="products")
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \Array
*
* @ORM\OneToMany(targetEntity="ProductOccurence", mappedBy="product")
*/
private $occurences;
/**
* @var \Array
*
* @ORM\OneToMany(targetEntity="Price", mappedBy="product")
*/
private $priceHistory;
Here's part of ProductOccurence.php
/**
* ProductOccurence
*
* @ORM\Table(name="product_occurences")
* @ORM\Entity
*/
class ProductOccurence
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="Product", inversedBy="occurences")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @var Shop
*
* @ORM\ManyToOne(targetEntity="Shop")
* @ORM\JoinColumn(name="shop_id", referencedColumnName="id")
*/
private $shop;
/**
* @var \Array
*
* @ORM\OneToMany(targetEntity="Price", mappedBy="occurence")
*/
private $prices;
Here's part of Price.php
/**
* Price
*
* @ORM\Table(name="prices")
* @ORM\Entity
*/
class Price
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="Product", inversedBy="priceHistory")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @var Shop
*
* @ORM\ManyToOne(targetEntity="Shop")
* @ORM\JoinColumn(name="shop_id", referencedColumnName="id")
*/
private $shop;
/**
* @var ProductOccurence
*
* @ORM\ManyToOne(targetEntity="ProductOccurence", inversedBy="prices")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_id", referencedColumnName="product_id"),
* @ORM\JoinColumn(name="shop_id", referencedColumnName="shop_id")
* })
*/
private $occurence;
You should specify a ManyToMany relationship for your $occurence
property, not a ManyToOne relationship.