2013-07-11 29 views
11

Próbuję skonfigurować niektóre relacje ManyToOne/OneToMany na obiektach w mojej bazie danych za pomocą Doctrine (2.2.3+) przez Symfony2 (2.3.0) i otrzymuję dziwny błąd. Oto odpowiednie części obiektów (wiele atrybutów do jednego produktu):Błąd powiązania 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; 
} 

Kiedy uruchomić komendę

php app/console doctrine:generate:entities BundleName 

pojawia się następujący błąd:

[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? 

mam przejrzałem dokumenty Doctrine i nie widzę żadnego odniesienia do instrukcji "use" dla parowania ManyToOne/OneToMany. Co się dzieje?

Odpowiedz

43

Składnia adnotacji nie jest kompletna.

Możesz zobaczyć poprawną składnię dowolnej adnotacji do doktryny poniżej.

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

W twoim przypadku powinien wyglądać tak:

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

Należy także poprawić adnotacje w jednostce ProductAttributes.

+0

Dzięki! Nadal pracuję z Symfony 2.7.3 –