Php 主键+;具有重复输入错误的外键

Php 主键+;具有重复输入错误的外键,php,doctrine-orm,Php,Doctrine Orm,我有两个相互关联的模型: /** @Entity @Table(name="permissions") */ class Permissions { /** * @Id @GeneratedValue @Column(type="integer") * @var integer */ protected $id; /** * @Column(type="string") * @var string */

我有两个相互关联的模型:

/** @Entity @Table(name="permissions") */
class Permissions {
    /**
     * @Id @GeneratedValue @Column(type="integer")
     * @var integer
     */
    protected $id;

    /**
     * @Column(type="string")
     * @var string
     */
    protected $name;

    public function getId() { return $this->id; }

    public function setName($name) { $this->name = $name; }
    public function getName() { return $this->name; }
}

当我想向Permissions添加两个具有值的实体时:

perm | type | name
-------------------
  1  |   0  | test1
  1  |   1  | test2
我明白了

键“UNIQ_12CF91AFFA6311EF”的重复条目“1”


第1列出现错误。我做错了什么?

这里有几个问题

  • 不能将相关实体用作另一实体的主键(
    @Id
  • 您正在使用一对一关系,但希望为每个
    权限添加多个
    权限类型。这需要一对多关联,最好是双向关联
  • 使用向权限添加类型

    /**
     * @OneToMany(targetEntity="PermissionTypes", mappedBy="perm")
     */
    protected $types;
    
    public function __construct()
    {
        $this->types = new \Doctrine\Common\Collections\ArrayCollection;
        // see http://www.doctrine-project.org/docs/orm/2.1/en/reference/association-mapping.html#collections
    }
    
    并将
    PermissionTypes
    更改为

    class PermissionsTypes {
    
        /**
         * @Id @GeneratedValue
         * @Column(type="integer")
         */
        protected $id;
    
        /**
         * @ManyToOne(targetEntity="Permissions", inversedBy="types")
         * @JoinColumn(name="perm_id", referencedColumnName="id")
         */
        protected $perm;
    

    你应该仔细阅读手册的这一部分。

    这里有几个问题

  • 不能将相关实体用作另一实体的主键(
    @Id
  • 您正在使用一对一关系,但希望为每个
    权限添加多个
    权限类型。这需要一对多关联,最好是双向关联
  • 使用向权限添加类型

    /**
     * @OneToMany(targetEntity="PermissionTypes", mappedBy="perm")
     */
    protected $types;
    
    public function __construct()
    {
        $this->types = new \Doctrine\Common\Collections\ArrayCollection;
        // see http://www.doctrine-project.org/docs/orm/2.1/en/reference/association-mapping.html#collections
    }
    
    并将
    PermissionTypes
    更改为

    class PermissionsTypes {
    
        /**
         * @Id @GeneratedValue
         * @Column(type="integer")
         */
        protected $id;
    
        /**
         * @ManyToOne(targetEntity="Permissions", inversedBy="types")
         * @JoinColumn(name="perm_id", referencedColumnName="id")
         */
        protected $perm;
    

    您应该仔细阅读本手册的章节。

    您能为PermissionsTypes表执行
    显示创建表\u name
    吗?看起来你在perm列上有一个唯一的键,而它应该在perm和type之间。是的,我在这个列上有唯一的键。问题是,它是如何创建的?您是如何创建表模式的?是手工生成的,还是自动生成的?@DigitalPrecision条令是一种ORM,很可能是用于生成schema@Phil是的,我知道。我试图弄清楚模式文件是具有唯一约束,还是实际的表本身。为了生成模式文件,表必须存在。如果他仅对perm列具有唯一约束,则会限制表充当M:M权限和类型之间关系的透视表的能力。我猜没关系,他接受了答案,所以提供的解决方案一定有效。你能为PermissionsTypes表做一个
    show create table\u name
    ?看起来你在perm列上有一个唯一的键,而它应该在perm和type之间。是的,我在这个列上有唯一的键。问题是,它是如何创建的?您是如何创建表模式的?是手工生成的,还是自动生成的?@DigitalPrecision条令是一种ORM,很可能是用于生成schema@Phil是的,我知道。我试图弄清楚模式文件是具有唯一约束,还是实际的表本身。为了生成模式文件,表必须存在。如果他仅对perm列具有唯一约束,则会限制表充当M:M权限和类型之间关系的透视表的能力。我猜没关系,他接受了答案,所以提供的解决方案一定有效。我不明白。你能更准确地解释一下吗?2.我将“一对一”更改为“manny to one”,结果成功:)。出现错误:[Doctrine\Common\Annotations\AnnotationException][语法错误]预期的Doctrine\Common\Annotations\DocLexer::T_CLOSE_括号,在属性权限的第43位得到了“mappedBy”:$types。@KrzysztofTrzos我在注释映射中遗漏了一些逗号,见我的最新答案你的1分不正确。请修改您的答案,因为我有一个类似的问题,并且条令文件明确表示您可以。1。我不明白。你能更准确地解释一下吗?2.我将“一对一”更改为“manny to one”,结果成功:)。出现错误:[Doctrine\Common\Annotations\AnnotationException][语法错误]预期的Doctrine\Common\Annotations\DocLexer::T_CLOSE_括号,在属性权限的第43位得到了“mappedBy”:$types。@KrzysztofTrzos我在注释映射中遗漏了一些逗号,见我的最新答案你的1分不正确。请修改你的答案,因为我有一个类似的问题,而且条令文件清楚地说你可以。