2013-07-11 12 views
11

Symfony2(2.3.0)を介してDoctrine(2.2.3+)を使用してデータベースのオブジェクトにManyToOne/OneToMany関係を設定しようとしていますが、奇妙なエラーが発生しています。Doctrine 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ペアの "use"ステートメントへの参照は見られません。何が起こっている?

答えて

43

注釈の構文が完全ではありません。

下記のdoctrineアノテーションの正しい構文を確認できます。

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

したがって、あなたのケースでは次のようになります。

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

また、ProductAttributesエンティティのアノテーションを修正することもできます。

+0

ありがとうございます! Symfony 2.7.3で作業しています –

関連する問題