drg5577 2014-11-23 11: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 00: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 : )

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部