2013-07-11 148 views
11

我想通过Symfony2(2.3.0)在我的数据库中使用Doctrine(2.2.3+)在对象上设置一些ManyToOne/OneToMany关系,并且出现一个奇怪的错误。下面是对象的相关部分(许多属性的一个产品):学说OneToMany关系错误

/** 
* Product 
* 
* @ORM\Table(name="product") 
* @ORM\Entity 
*/ 
class Product 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    ... 

    /** 
    * 
    * @OneToMany(targetEntity="ProductAttributes", mappedBy="product") 
    */ 
    protected $product_attributes; 

    public function __construct() { 
     $this->product_attributes = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 
} 

/** 
* ProductAttributes 
* 
* @ORM\Table(name="product_attributes") 
* @ORM\Entity 
*/ 
class ProductAttributes 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="pa_id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $pa_id; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="product_id", type="integer") 
    */ 
    protected $product_id; 

    ... 

    /** 
    * 
    * @ManyToOne(targetEntity="Product", inversedBy="product_attributes") 
    * @JoinColumn(name="product_id", referencedColumnName="id") 
    */ 
    protected $product; 
} 

当我运行

php app/console doctrine:generate:entities BundleName 

命令我碰到下面的错误:

[Doctrine\Common\Annotations\AnnotationException]                            
[Semantical Error] The annotation "@OneToMany" in property LVMount\LVMBundle\Entity\Product::$product_attributes was never imported. Did you maybe forget to add a "use" statement for this annotation? 

我有查看了Doctrine文档,并没有看到任何关于ManyToOne/OneToMany配对的“使用”声明。到底是怎么回事?

回答

43

您的注释语法不完整。

您可以在下面看到任何教义注释的正确语法。

/** 
* @ORM\******** 
*/ 

因此,在你的情况下,它应该看起来像下面这样。

/** 
* @ORM\OneToMany(targetEntity="ProductAttributes", mappedBy="product") 
*/ 

您还需要修正ProductAttributes实体中的注释。

+0

谢谢!仍然在使用Symfony 2.7.3 –