У меня есть таблица author и у неё есть связь один ко многим. Я хотел бы сделать сортировку и поиск по количеству связанных моделей books, но пока понял только, как их получить и отобразить, если может кто помочь, помогите пожалуйста.
<?php
namespace frontend\models;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "authors".
*
* @property int $id
* @property string $name
* @property string $patronymic
* @property string $surname
* @property string $created_at
* @property string $updated_at
*
* @property Books[] $books
*/
class Author extends ActiveRecord
{
public static function tableName()
{
return 'authors';
}
public function rules()
{
return [
[['name', 'created_at', 'updated_at'], 'required'],
[['created_at', 'updated_at'], 'safe'],
[['name', 'patronymic', 'surname'], 'string', 'max' => 255],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'patronymic' => 'Patronymic',
'surname' => 'Surname',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
public function getBooks()
{
return $this->hasMany(Books::className(), ['author_id' => 'id']);
}
public function getAmountBooks()
{
return $this->getBooks()->count();
}
/*public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}*/
}
<?php
namespace frontend\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Author;
/**
* SearchAuthor represents the model behind the search form of `frontend\models\author`.
*/
class SearchAuthor extends Author
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id'], 'integer'],
[['name', 'patronymic', 'surname', 'created_at', 'updated_at'], '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 = Author::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'patronymic', $this->patronymic])
->andFilterWhere(['like', 'surname', $this->surname]);
return $dataProvider;
}
}
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel frontend\models\SearchAuthor */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Authors';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="author-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create author', ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Create book', ['books/create'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Library', ['books/index'], ['class' => 'btn btn-warning']) ?>
</p>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'patronymic',
'surname',
'created_at',
//'updated_at',
[
'attribute'=>'id',
'label'=>'Amount Books',
'content'=>function($data) {
return $data->getAmountBooks();
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
<?php
namespace frontend\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Author;
/**
* SearchAuthor represents the model behind the search form of `frontend\models\author`.
*/
class SearchAuthor extends Author
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id'], 'integer'],
[['name', 'patronymic', 'surname', 'created_at', 'updated_at'], '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 = Author::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'patronymic', $this->patronymic])
->andFilterWhere(['like', 'surname', $this->surname]);
return $dataProvider;
}
}