我试图通过Symfony2(2.3.0)使用Doctrine(2.2.3+)在数据库中的对象上设置一些ManyToOne/OneToMany关系,但遇到了一个奇怪的错误。以下是对象的相关部分(一个产品的许多属性):
/**
* Product
*
* @ORMTable(name="product")
* @ORMEntity
*/
class Product
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
...
/**
*
* @OneToMany(targetEntity="ProductAttributes", mappedBy="product")
*/
protected $product_attributes;
public function __construct() {
$this->product_attributes = new DoctrineCommonCollectionsArrayCollection();
}
}
/**
* ProductAttributes
*
* @ORMTable(name="product_attributes")
* @ORMEntity
*/
class ProductAttributes
{
/**
* @var integer
*
* @ORMColumn(name="pa_id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $pa_id;
/**
* @var integer
*
* @ORMColumn(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
命令我得到以下错误:
[DoctrineCommonAnnotationsAnnotationException]
[Semantical Error] The annotation "@OneToMany" in property LVMountLVMBundleEntityProduct::$product_attributes was never imported. Did you maybe forget to add a "use" statement for this annotation?
我看过Doctrine文档,没有看到任何关于ManyToOne/OneToMany配对的"使用"声明。发生了什么事?
注释的语法不完整。
您可以在下面看到任何学说注释的正确语法。
/**
* @ORM********
*/
因此,在您的情况下,它应该如下所示。
/**
* @ORMOneToMany(targetEntity="ProductAttributes", mappedBy="product")
*/
您还需要修复ProductAttributes
实体中的注释。