Answer the question
In order to leave comments, you need to log in
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']);
}
}
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Genre extends ActiveRecord {
public function getGenre() {
return $this->hasMany(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 = new Genre;
$query = $genres->getBook()
->orderBy('id_genre')
->all();
$books = $query;
return $this->render('book', [
'books' => $books,
]);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question