authors: id | name | book_quant
books: id | name | price |author
В authors хранится id автора. На странице с книгами в GridView в графе "автор" должно выводиться имя name из таблицы authors. Вроде всё правильно делаю, но в графе почему-то (not set)
Books.php
Код: Выделить всё
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "books".
*
* @property int $id
* @property string $name
* @property int $price
* @property int $author
*/
class Books extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'books';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'price'], 'required'],
[['price', 'author'], 'integer'],
[['name'], 'string', 'max' => 200],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'price' => 'Price',
'author' => 'Author',
];
}
public function getAuthor(){
return $this->hasOne(Authors::className(),['id'=>'author']);
}
}
Код: Выделить всё
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Books;
/**
* BooksSearch represents the model behind the search form of `app\models\Books`.
*/
class BooksSearch extends Books
{
public $authorName;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'price', 'author'], 'integer'],
[['name', 'authorName'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Books::find();
$query->joinWith(['author']);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['authorName'] = [
'asc' => [Authors::tableName().'.name' => SORT_ASC],
'desc' => [Authors::tableName().'.name' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'price' => $this->price,
'author' => $this->author,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
$query->andFilterWhere(['like', Authors::tableName().'.name', $this->authorName]);
return $dataProvider;
}
}
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\models\BooksSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Книги';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="books-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Books', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
['attribute' => 'name', 'label'=>'Название'],
['attribute' => 'price', 'label'=>'Цена'],
['attribute' => 'authorName','label' => 'Автор', 'value'=>'author.name'],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>