Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine OneToMany relationship error

I am trying to set up some ManyToOne/OneToMany relationships on objects in my database using Doctrine (2.2.3+) via Symfony2 (2.3.0) and am getting a strange error. Here are the relevant parts of the objects (many attributes to one product):

/**
 * 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;
}

When I run the

php app/console doctrine:generate:entities BundleName

command I get the following error:

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

I have looked through the Doctrine docs and don't see any reference to a "use" statement for the ManyToOne/OneToMany pairings. What is going on?

like image 708
Aaron Luman Avatar asked Jul 11 '13 00:07

Aaron Luman


1 Answers

Your annotations' syntax aren't complete.

You can see the proper syntax for any doctrine annotation below.

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

So, in your case it should look like the following.

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

You will also want to fix the annotations in the ProductAttributes entity.

like image 198
Thomas Potaire Avatar answered Nov 13 '22 12:11

Thomas Potaire