我有两个彼此相关的模型:
/** @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; }
}
和
/** @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; }
}
当我想向PermissionsTypes添加两个具有值的实体时:
perm | type | name
-------------------
1 | 0 | test1
1 | 1 | test2
我得到了
第1列上的密钥'UNIQ_12CF91AFFA6311EF'重复输入'1'
错误。我做错了什么?
答案 0 :(得分:1)
这里有几个问题......
@Id
)PermissionTypes
个添加多个Permissions
。这需要一对多关联,最好是双向关联。使用
将类型添加到Permissions
/**
* @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;
您应该仔细阅读本手册的Association Mapping部分。