学说问题:无法获得最后一个插入标识符

时间:2010-04-12 10:56:34

标签: orm doctrine

当我尝试将数据保存到我的模型时,Doctrine会抛出此异常:

Message: Couldn't get last insert identifier. 

我的表格设置代码是:

$this->hasColumn('id', 'integer', 4, array(
         'type' => 'integer',
         'length' => 4,
         'fixed' => false,
         'unsigned' => false,
         'primary' => true,
         'autoincrement' => true,
         ));

请帮忙。感谢。

3 个答案:

答案 0 :(得分:15)

检查以确保数据库中的列设置为auto_increment。看起来Doctrine类将它作为auto_increment处理,但它不是在DB中设置的。

答案 1 :(得分:2)

对我来说,问题是default参数。

        $this->hasColumn('inscription_id', 'integer', 4, array(
         'type' => 'integer',
         'length' => 4,
         'fixed' => false,
         'unsigned' => false,
         'primary' => true,
 //        'default' => '0', !!! get "couldn't get last inserted identifier doctrine"
         'notnull' => true,
         'autoincrement' => true,
         ));

答案 2 :(得分:1)

这对我有用:

$this->hasColumn('cd_fabricante', 'integer', 4, array(
          'type' => 'integer',
          'length' => 4,
          'unsigned' => true,
          'primary' => true,
          'auto_increment' => true,
) );

与之前的参数相同,也是同样的错误。

编辑:我最近发现了将“auto_increment”添加到PK列定义中,现在我的行​​为与Doctrine处理的任何给定ID字段的行为相同,无论我选择什么名称