嗨,我有这个设置:N-1位用户N-1组N-N位角色
describe 'block1' do
before do
#setup here
end
it 'test1' do
end
...
end
describe 'block2' do
it 'test50' do
end
end
我已经有很多角色,组和用户,并且当我使用以下内容更新House对象时:
Domain\Entity\House:
manyToOne:
user:
targetEntity: Domain\Entity\User
cascade: ["persist"]
inversedBy: houses
joinColumn:
name: user_id
referencedColumnName: id
onDelete: CASCADE
genre:
targetEntity: Domain\Entity\Genre
cascade: ["persist"]
inversedBy: houses
joinColumn:
name: genre_id
referencedColumnName: id
onDelete: SET NULL
Domain\Entity\User:
oneToMany:
houses:
targetEntity: Domain\Entity\House
cascade: ["persist"]
mappedBy: user
manyToOne:
group:
targetEntity: Domain\Entity\Group
inversedBy: users
Domain\Entity\Group:
manyToMany:
roles:
targetEntity: Domain\Entity\Role
joinTable:
name: groups_roles
joinColumns:
group_id:
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
role_id:
referencedColumnName: id
onDelete: CASCADE
一切都爆炸了,学说告诉我:
通过“ User#group”关系发现了一个新实体,该关系未配置为级联实体的持久化操作
所以我已经编辑了映射以具有 cascade = [“ persist”] 选项:
$userObj = $userRepo->find(3);
$house->setUser($userObj);
$houseRepo->save($house);
但是当我尝试运行上面的php代码以保存更新的House对象时,出现此错误
使用参数['ROLE_USER',null]执行'INSERT INTO角色(代码,描述)VALUES(?,?)'时发生异常: SQLSTATE [23000]:违反完整性约束:1062复制条目 密钥“ UNIQ_B63E2EC777153098”的“ ROLE_USER”
使用参数[null,1,null]执行'INSERT INTO genres(name,active,parent_id)VALUES(?,?,?)'时发生异常: SQLSTATE [23000]:违反完整性约束:1048列“名称”不能为空
为什么Doctrine不知道分配给组的角色,分配给用户的角色已经存在于我的数据库中?每个数据已经存在于数据库中。
我的基本存储库正在使用以下代码保存实体:
Domain\Entity\House:
manyToOne:
user:
targetEntity: Domain\Entity\User
cascade: ["persist"]
inversedBy: houses
joinColumn:
name: user_id
referencedColumnName: id
onDelete: CASCADE
genre:
targetEntity: Domain\Entity\Genre
cascade: ["persist"]
inversedBy: houses
joinColumn:
name: genre_id
referencedColumnName: id
onDelete: SET NULL
Domain\Entity\User:
oneToMany:
houses:
targetEntity: Domain\Entity\House
cascade: ["persist"]
mappedBy: user
manyToOne:
group:
targetEntity: Domain\Entity\Group
inversedBy: users
cascade: ["persist"]
Domain\Entity\Group:
manyToMany:
roles:
cascade: ["persist"]
targetEntity: Domain\Entity\Role
joinTable:
name: groups_roles
joinColumns:
group_id:
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
role_id:
referencedColumnName: id
onDelete: CASCADE
自2017年2月以来,我一直在使用此基本存储库来持久存储新的和更新的实体,而我完全没有问题
房屋对象实例的快照,您可以看到他的用户以及他的用户组和组的角色