thinkPhp6 模型新增不能取得主键

浏览:787 发布日期:2020/04/05 分类:ThinkPHP5专区 关键字: thinkphp6 模型新增 主键
$result = $db->strict(false)
                ->field($allowFields)
                ->replace($this->replace)
                ->insert($this->data, false, $sequence);

            // 获取自动增长主键
            if ($result && $insertId = $db->getLastInsID($sequence)) {
                $pk = $this->getPk();

                if (is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) {
                    $this->data[$pk] = $insertId;
                }
            }
这里的insert 会查询一次主键 ,然后getLastInsID 再查询的时候就是0了
提供一个临时解决方法
vendor\topthink\think-orm\src\db\PDOConnection.php->insert()// $lastInsId = $this->getLastInsID($query, $sequence);
$lastInsId = 0;
使用 create 就可以获得主键了

如果更改了之后遇到其他问题 欢迎指正
最佳答案
评论( 相关
后面还有条评论,点击查看>>