doushi1996 2011-11-29 21:13
浏览 21
已采纳

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

I have two models which are related to each other:

/** @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; }
}

and

/** @Entity @Table(name="permissions_types") */
class PermissionsTypes {
    /**
     * @Id
     * @OneToOne(targetEntity="Permissions")
     * @JoinColumn(name="perm_id", referencedColumnName="id")
     */
    protected $perm;

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

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

    public function setType($type) { $this->type = $type; }
    public function getType() { return $this->type; }

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

When I want to add to PermissionsTypes two entities with values:

perm | type | name
-------------------
  1  |   0  | test1
  1  |   1  | test2

I get

Duplicate entry '1' for key 'UNIQ_12CF91AFFA6311EF'

error on the 1st column. What I do wrong?

  • 写回答

1条回答 默认 最新

  • dpy83214 2011-11-29 21:25
    关注

    Several problems here...

    1. You can't use a related entity as another entity's primary key (@Id)
    2. You're using a one-to-one relationship but want to add more than one PermissionTypes per Permissions. This requires a one-to-many association, preferably bi-directional.

    Add types to Permissions with

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

    and change PermissionTypes to

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

    You should read the Association Mapping section of the manual carefully.

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)