duan62819774 2010-08-27 14:06
浏览 38
已采纳

PHP主义多对多的最后一个标识符问题

I have small problem when trying to create many-to-many link in doctrine.

I have simple scenario here: User and Role with many-to-many relationship defined.

$user = new User();
$user->username = 'greg';
$user->save();


$role = new Role();
$role->name = "Administrator";
$role->save();

$role_user = new Role_User();
$role_user->Role = $role;
$role_user->User = $user;
$role_user->save();

Now, I did some experiments and I can save user and role on it's own without any problems. They both have autoincrement ids and it works fine. The problem starts when I want to create relationship with exception saying 'Couldn't get last insert identifier'.

I can't change DB schema and I was not generating it via YAML - I reverse engineered models from existing DB via 'generateModelsFromDb'. This is how this table looks like:

CREATE TABLE  `ofp`.`Role_User` (
   `roles_id` bigint(20) NOT NULL,
   `users_id` bigint(20) NOT NULL,
   KEY `FK8B6B91F429A30245` (`roles_id`),
   KEY `FK8B6B91F429A6266F` (`users_id`),
   CONSTRAINT `FK8B6B91F429A6266F` FOREIGN KEY (`users_id`) REFERENCES `User` (`id`),
   CONSTRAINT `FK8B6B91F429A30245` FOREIGN KEY (`roles_id`) REFERENCES `Role` (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Error I'm getting would suggest there is something wrong with creating primary key identifier to this table, but I'm not sure I need one there (Maybe unique key to eliminate duplicates?).

Does anybody have any clues?

  • 写回答

2条回答 默认 最新

  • dptrmt4366 2010-08-28 16:45
    关注

    Setting refClass is mandatory in many-to-many relationships

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题