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

时间:2022-08-01 20:10:25
INSERT INTO `table1`
SET `field1` = 'value1',
 `field2` = 'value2',
 `field3` = 'value3';

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

i'm trying to use DB::query(Database::INSERT,$query); - it doesn't work

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

$query - i write on the top of message

$ query - 我写在消息的顶部

1 个解决方案

#1


0  

Use ORM module like:

使用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();

#1


0  

Use ORM module like:

使用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();