Answer the question
In order to leave comments, you need to log in
How to refer in Yii2 migrations to inserts from the code above?
What is the best practice in yii2 migrations for inserting data into one table and then using the id of this row to insert into rows of another table as a key? How to get this id? Are there links like those in the symphony migration?
And is it possible to insert a row into a table and then insert a key on it in another row of the same table?
Answer the question
In order to leave comments, you need to log in
I don't know for bulk adding, but for a single one, you can use $this->db->lastInsertID.
Haven't tested it, but it should work somehow...
foreach ($persons as $person) {
$this->insert('person', [
'name' => $person->name
]);
$personId = $this->db->lastInsertID;
$this->insert('profile', [
'phone' => $person->phone,
'personId' => $personId,
]);
}
It seems that in this case it is better not to use migrations, but to generate data, for example, with a command for some array.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question