[英]Doctrine one-to-many relation - “No identifier/primary key specified”
Doctrine fails with a simple bi-directional many-to-one relationship between FoodDes (many) and FoodGroup (one). 教义失败,因为FoodDes(许多)和FoodGroup(一个)之间存在简单的双向多对一关系。 Both entities are shown here:
这两个实体都显示在这里:
/**
* @ORM\Entity
* @ORM\Table(name="FOOD_DES")
*/
class FoodDes
{
public function __construct()
{
$this->foodGroup = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(name="NDB_No", type="string", length=10)
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="FoodGroup", inversedBy="fdGroupCode")
* @ORM\JoinColumn(name="FdGrp_Cd", referencedColumnName="FdGrp_CD")
*/
protected $foodGroup;
}
> >
/**
* @ORM\Entity
* @ORM\Table(name="FD_GROUP")
*/
class FoodGroup
{
/**
* @ORM\Id();
* @ORM\GeneratedValue(strategy="NONE");
* @ORM\OneToMany(targetEntity="FoodDes", mappedBy="foodGroup")
*/
protected $fdGroupCode;
When I run doctrine orm:schema-tool:create, it fails with error: 当我运行教义orm:schema-tool:create时,它失败并显示错误:
No identifier/primary key specified for Entity 'Acme\\Entities\\FoodGroup'.
没有为实体“ Acme \\ Entities \\ FoodGroup”指定标识符/主键。 Every Entity must have an identifier/primary key.
每个实体必须具有一个标识符/主键。
However, I labeled $fdGroupCode as my only identifier. 但是,我将$ fdGroupCode标记为唯一标识符。
I've also tried creating a new primary key $id on the FoodGroup entity and removing the primary key label from $fdGroupCode on FoodGroup. 我还尝试了在FoodGroup实体上创建新的主键$ id并从FoodGroup的$ fdGroupCode中删除主键标签。 Below is the new FoodGroup entity.
以下是新的FoodGroup实体。
/**
* @ORM\Entity
* @ORM\Table(name="FD_GROUP")
*/
class FoodGroup
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="FoodDes", mappedBy="foodGroup")
*/
protected $fdGroupCode;
When I run doctrine orm:schema-tool:create again, it results with a new error: 当我再次运行教义orm:schema-tool:create时,结果出现新错误:
[Doctrine\\ORM\\ORMException]
[学说\\ ORM \\ ORMException]
Column nameFdGrp_CD
referenced for relation from Acme\\Entities\\FoodDes towards Acme\\Entities\\FoodGroup does not exist.从Acme \\ Entities \\ FoodDes指向Acme \\ Entities \\ FoodGroup的关系引用的列名
FdGrp_CD
不存在。
This error doesn't make any sense. 此错误没有任何意义。 Of course it wouldn't exist.
当然不会存在。 I am running it against an empty database!
我正在针对空数据库运行它!
These error occur running from the command line, but they also occur when querying the entities against a database. 这些错误是从命令行运行时发生的,但是在针对数据库查询实体时也会发生。 Can somebody please help me?
有人能帮帮我吗?
I'd rather give you working example of OneToMany from one of my projects, so you can see the difference and format code in proper way. 我希望从我的一个项目中为您提供OneToMany的工作示例,以便您可以正确地查看差异和格式代码。 If it does not work, then try to get a new
Symfony
dist and start over. 如果不起作用,请尝试获取新的
Symfony
dist并重新开始。
<?php
// SomeBundle/Entity/Shop/Product.php
namespace SomeBundle\Entity\Shop;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="shop_products")
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="bigint")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="ProductItem", mappedBy="product")
*/
protected $productItem;
}
Related entity: 相关实体:
<?php
// SomeBundle/Entity/Shop/ProductItem.php
namespace SomeBundle\Entity\Shop;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="shop_products_items")
*/
class ProductItem
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="bigint")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="productItem")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
}
For reasons why your code does not work could be many (namespaces, folder structure, column names, etc.). 由于某些原因,您的代码无法正常工作(命名空间,文件夹结构,列名等)。 This example works and tested.
此示例有效且经过测试。 Give it a try : )
试试看 : )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.