【问题标题】:Kohana DB last insert id before insertKohana DB 插入前的最后插入 id
【发布时间】:2014-02-19 12:20:50
【问题描述】:

我有一个带有自动增量 ID 的表格。

我要根据它的id prior the insert来生成序列号,我能找到最高的ID值,但是如果之前有delete的话,还有AUTO INCREMENT的

table != max(id)+1

【问题讨论】:

    标签: mysql kohana-3 kohana-orm


    【解决方案1】:

    将其分解为两个查询。

    1. 运行插入查询以检索 id。

    2. 根据检索到的 id 创建连续剧。

    3. 使用更新查询将序列值添加到记录中。

    使用 ORM 模块的示例代码

    $my_table = ORM::factory('Mytable');
    
    $my_table->field1 = 'Value1';
    $my_table->field2 = 'Value2';
    
    $my_table->save(); // inserts record
    
    // Create serials (will obviously be more complex than this)  
    $my_table->serial = $my_table->id;
    
    $my_table->save(); // updates record
    

    使用数据库模块的示例代码

    list($id) = DB::insert('my_table', array('field1','field2'))  
    ->values(array('value1','value2')  
    ->execute();
    
    // Create serials (will obviously be more complex than this)  
    $serial = $id;
    
    DB::update('my_table')  
    ->set(array('serial' => $serial))  
    ->where('id', '=', $id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      相关资源
      最近更新 更多