学说的一对多关系错误 [英] Doctrine OneToMany relationship error

查看:77
本文介绍了学说的一对多关系错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Symfony2(2.3.0)使用Doctrine(2.2.3+)在我的数据库中的对象上建立一些ManyToOne/OneToMany关系,并且遇到一个奇怪的错误.以下是对象的相关部分(一种产品的许多属性):

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

当我运行

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"语句的任何引用.发生了什么事?

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?

推荐答案

您注释的语法不完整.

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

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")
 */

您还需要修复ProductAttributes实体中的注释.

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

这篇关于学说的一对多关系错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆