kohana db首先在表2中使用表1中的LAST_INSERT_ID()插入表1和第二个插入

时间:2013-05-25 20:05:22

标签: kohana kohana-3 kohana-db

INSERT INTO `table1`
SET `field1` = 'value1',
 `field2` = 'value2',
 `field3` = 'value3';

INSERT INTO `table2`
SET id = (SELECT LAST_INSERT_ID()),
 `field` = 'value';

我正在尝试使用DB::query(Database::INSERT,$query); - 它不起作用

$ query - 我写在消息顶部

1 个答案:

答案 0 :(得分:0)

使用ORM模块,如:

// We make first insert
$table1_orm = ORM::factory('Item') // `table1`
    ->values(array(
        'field1' => 'value1',
        'field2' => 'value2',
        'field3' => 'value3',
    ))
    ->save(); // Now saved Model is in $table1_orm variable. ID is in $table1_orm->id

// Now second insert
ORM::factory('Item2') // `table2`
    ->values(array(
        'id' => $table1_orm->id,
        'field' => 'value'
    ))
    ->save();