在没有主键的情况下将新记录插入 table (CakePHP)
Insert new record to table without primary key (CakePHP)
我需要将 PhpBB 与 CakePHP 集成。在 PhpBB 中有 table phpbb_user_group 没有主键:
group_id | user_id | group_leader | user_pending
当我创建新用户时,我需要向此 table 添加新记录,但 CakePHP 给我以下错误:
Cannot insert row in "phpbb_user_group" table, it has no primary key.
我可以将带有主键的列添加到 table,但我不想这样做。更新 PhpBB 总是很痛苦,我预见到未来会出现问题,所以我宁愿不修改数据库结构。
在 CakePHP 中是否可以在没有主键的情况下向 table 插入新记录?
您必须 "manually" 创建和 运行 INSERT
语句,例如使用查询构建器,您将无法使用 ORM 的保存功能,因为它依赖于存在的主键。
$statement = $Table
->query()
->insert([
'group_id',
'user_id',
'group_leader',
'user_pending'
])
->values([
'group_id' => $groupId,
'user_id' => $userId,
'group_leader' => $groupLeader,
'user_pending' => $userPending
])
->execute();
$success = $statement->rowCount() === 1;
$statement->closeCursor();
另见
我需要将 PhpBB 与 CakePHP 集成。在 PhpBB 中有 table phpbb_user_group 没有主键:
group_id | user_id | group_leader | user_pending
当我创建新用户时,我需要向此 table 添加新记录,但 CakePHP 给我以下错误:
Cannot insert row in "phpbb_user_group" table, it has no primary key.
我可以将带有主键的列添加到 table,但我不想这样做。更新 PhpBB 总是很痛苦,我预见到未来会出现问题,所以我宁愿不修改数据库结构。
在 CakePHP 中是否可以在没有主键的情况下向 table 插入新记录?
您必须 "manually" 创建和 运行 INSERT
语句,例如使用查询构建器,您将无法使用 ORM 的保存功能,因为它依赖于存在的主键。
$statement = $Table
->query()
->insert([
'group_id',
'user_id',
'group_leader',
'user_pending'
])
->values([
'group_id' => $groupId,
'user_id' => $userId,
'group_leader' => $groupLeader,
'user_pending' => $userPending
])
->execute();
$success = $statement->rowCount() === 1;
$statement->closeCursor();
另见