user2368299
user2368299

Reputation: 369

kohana db first insert table 1 and second insert in table 2 with LAST_INSERT_ID() from table 1

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

$query - i write on the top of message

Upvotes: 0

Views: 126

Answers (1)

s.webbandit
s.webbandit

Reputation: 17000

Use ORM module like:

// 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();

Upvotes: 0

Related Questions