<?php
namespace app\models;
use yii\db\ActiveRecord;
class Genre extends ActiveRecord {
public function getBooks() {
return $this->hasMany(Book::className(), ['id_genre' => 'id_genre']);
}
}
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Book extends ActiveRecord {
public function getGenre() {
return $this->hasOne(Genre::className(), ['id_genre' => 'id_genre']);
}
}
<?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 = Genre::find()->all();
$books = $genres->book;
return $this->render('book', [
'books' => $books,
]);
}
}