【问题标题】:Doctrine OneToMany relationship errorDoctrine OneToMany 关系错误
【发布时间】:2013-07-11 00:39:11
【问题描述】:

我正在尝试通过 Symfony2 (2.3.0) 使用 Doctrine (2.2.3+) 在我的数据库中的对象上设置一些 ManyToOne/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 配对的“使用”语句的任何引用。怎么回事?

【问题讨论】:

    标签: symfony doctrine-orm one-to-many many-to-one


    【解决方案1】:

    您的注释语法不完整。

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

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

    因此,在您的情况下,它应该如下所示。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      相关资源
      最近更新 更多