T
T
this__all2020-04-27 15:07:34
Yii
this__all, 2020-04-27 15:07:34

How to merge tables in Yii?

I am trying to merge two tables in Yii. But I can't figure out how to implement it correctly.

Book Model

<?php
namespace app\models;
use yii\db\ActiveRecord;

class Book extends ActiveRecord {
  public function getBook() {
    return $this->hasOne(Book::className(), ['id_genre' => 'id_genre']);
  }
}


Genre Model

<?php
namespace app\models;
use yii\db\ActiveRecord;

class Genre extends ActiveRecord {
  public function getGenre() {
    return $this->hasMany(Genre::className(), ['id_genre' => 'id_genre']);
  }
}


Book Controller

<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Book;
use app\models\Genre;

class BookController extends Controller {
    public function actionBook() {
        $genres = new Genre;
        $query = $genres->getBook()
          ->orderBy('id_genre')
          ->all();

        $books = $query;

        return $this->render('book', [
            'books' => $books,
        ]);
    }
}


Database

5ea6caf6c6ac2265763734.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question