【问题标题】:Doctrine Composite Keys Throwing Error教义复合键抛出错误
【发布时间】:2016-07-01 06:45:14
【问题描述】:

我正在尝试使用以下方法更新我的学说架构:

php vendor/bin/doctrine orm:schema-tool:update

但我收到错误消息:

[Doctrine\ORM\Mapping\MappingException]                                      
  Single id is not allowed on composite primary key in entity entities\Events

事件实体如下所示:

<?php
// entities/Events.php

namespace entities;

use Entities\Enities;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\Mapping\Table;

/**
 * @Entity
 * @Table(name="development.events")
 **/
class Events extends Entities {
    /**
     * @var integer
     *
     * @Id
     * @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $event_id;

    /**
     * @var integer
     *
     * @Id
     * @Column(name="app_id", type="integer")
     */
    protected $app_id = 0;

    /**
     * @var string
     * @Column(type="string", length=64)
     */
    protected $post_title;

    public function __construct($event, $app){
        $this->event_id = $event;
        $this->app_id= $app;
    }

}

根据文档,本机支持复合键。 http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html我做错了什么?

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案1】:

    来自文档:

    每个具有复合键的实体都不能使用“ASSIGNED”以外的 id 生成器。这意味着必须在调用 EntityManager#persist($entity) 之前设置 ID 字段的值。

    所以删除@GeneratedValue(strategy="AUTO")

    【讨论】:

    • 它有效,但这是否意味着对于该值,我必须以编程方式使用 ID 或 UUID,而不是让数据库创建自动递增的值?
    • 是的,或者你可以使用#identity-through-foreign-entities,但我真的不明白你的用例是什么,你想实现什么?不建议使用复合键,应尽可能避免使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多