drg5577 2014-11-23 19:32
浏览 54
已采纳

Doctrine一对多关系 - “未指定标识符/主键”

Doctrine fails with a simple bi-directional many-to-one relationship between FoodDes (many) and FoodGroup (one). 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:

No identifier/primary key specified for Entity 'Acme\Entities\FoodGroup'. Every Entity must have an identifier/primary key.

However, I labeled $fdGroupCode as my only identifier.


Next approach

I've also tried creating a new primary key $id on the FoodGroup entity and removing the primary key label from $fdGroupCode on FoodGroup. Below is the new FoodGroup entity.

/**
 * @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:

[Doctrine\ORM\ORMException]
Column name FdGrp_CD referenced for relation from Acme\Entities\FoodDes towards Acme\Entities\FoodGroup does not exist.

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?

  • 写回答

1条回答 默认 最新

  • dongliling6336 2014-11-24 08:27
    关注

    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. If it does not work, then try to get a new Symfony dist and start over.

    <?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 : )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存