Doctrine OneToMany relationship error
Asked Answered
D

1

11

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?

Dealing answered 11/7, 2013 at 0:39 Comment(0)
H
44

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.

Hep answered 11/7, 2013 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.