【问题标题】:Insert new record to table without primary key (CakePHP)在没有主键的情况下向表中插入新记录(CakePHP)
【发布时间】:2019-05-15 12:45:52
【问题描述】:

我需要将 PhpBB 与 CakePHP 集成。而在 PhpBB 中有没有主键的表 phpbb_user_group:

group_id | user_id | group_leader | user_pending    

当我创建新用户时,我需要向这个表中添加新记录,但是 CakePHP 给了我以下错误:

Cannot insert row in "phpbb_user_group" table, it has no primary key. 

我可以将带有主键的列添加到表中,但我不想这样做。更新 PhpBB 总是让人头疼,而且我预见到未来会出现问题,所以我宁愿不修改数据库结构。

CakePHP 中是否可以在没有主键的情况下向表中插入新记录?

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    您必须“手动”创建和运行 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();
    

    另见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多