基本理念:我有4个Doctrine实体:Product,Shop,ProductOccurence和Price。产品出现在一个或多个商店中,价格与产品和商店相关。
因此,例如笔记本电脑A(a Product
)由商店A和商店B销售(因此数据库中有2个ProductOccurence
个实例,链接到同一个Product
,但是不同的Shop
实体)并且在商店A中有3个列表价格,在商店B中有4个价格(因此实体Price
的表中有7个实例)。
当我学说:schema:create时,我收到了这个错误:
[Doctrine\ORM\ORMException]
Column name `product_id` referenced for relation from TRP\ModelBundle\Entity\Price towards TRP\ModelBundle\Entity\ProductOccurence does not exist.
删除Price::$occurence
(具有多个连接列的注释)的注释可以解决问题,但我想了解它首先出现的原因。谢谢!
这是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;
这是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;
这是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;