S
S
Sergey Beloventsev2018-05-07 22:33:01
Yii
Sergey Beloventsev, 2018-05-07 22:33:01

How to make filling with migrations in a many-to-many relationship?

There are three tables Autor, Book and BookAutor. The Autor table is populated like this

$this->insert(Autor::tableName(), [
            'first_name' => 'Рей',
            'second_name' => 'Бредбери',
        ]);

this is how the Book table is filled
$this->insert(Book::tableName(), [
            'title' => '451 градус по Фаренгейту',
        ]);

now I would like to know the id of the filled tables in order to fill the BookAutor table with the
book_id and autor_id values

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2018-05-08
@VladimirAndreev

and what prevents in migrations to create instances of Author and Book?
if it is not imported by the millions, of course.
but in general, it seems that you need imports, not migrations ...

L
LAV45, 2018-05-18
@LAV45

$id = Yii::$app->db->schema
    ->insert(Autor::tableName(), [
        'first_name' => 'Рей',
        'second_name' => 'Бредбери',
    ]);

or
$this->insert(Autor::tableName(), [
    'first_name' => 'Рей',
    'second_name' => 'Бредбери',
]);
$id = Yii::$app->db->schema->getLastInsertID()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question