Пример простой - есть таблица продуктов и есть таблица категорий. в таблице продуктов есть ссылка на id категории.
Product
Код: Выделить всё
<?php
namespace common\models\product;
use Yii;
use yii\db\ActiveRecord;
use common\models\category\Category;
class Product extends ActiveRecord
{
public static function tableName()
{
return 'product';
}
public function rules()
{
return [
[['base_id'], 'integer'],
[['category'], 'safe'],
[['cloth', 'title', 'description', 'photo_1', 'photo_2', 'photo_3', 'photo_4'], 'string', 'max' => 255],
//[['category'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category' => 'id']],
];
}
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'Базовое ID'),
'base_id' => Yii::t('app', 'Экспортное ID'),
'category' => Yii::t('app', 'Категория'),
'cloth' => Yii::t('app', 'Категория ткани'),
'title' => Yii::t('app', 'Наименование продукта'),
'description' => Yii::t('app', 'Описание продукта'),
'photo_1' => Yii::t('app', 'Photo 1'),
'photo_2' => Yii::t('app', 'Photo 2'),
'photo_3' => Yii::t('app', 'Photo 3'),
'photo_4' => Yii::t('app', 'Photo 4'),
];
}
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category']);
}
}
Код: Выделить всё
<?php
namespace common\models\product;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\product\Product;
class ProductSearch extends Product
{
public $categoryName;
public function rules()
{
return [
[['id', 'base_id', 'category'], 'integer'],
[['cloth', 'title', 'description', 'photo_1', 'photo_2', 'photo_3', 'photo_4'], 'safe'],
[['categoryName'], 'safe'],
];
}
/*
public function scenarios()
{
return Model::scenarios();
}
*/
public function search($params)
{
$query = Product::find();
$query->joinWith(['category']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['categoryName'] = [
'asc' => ['category.name' => SORT_ASC],
'desc' => ['category.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;
}
return $dataProvider;
}
}
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use common\models\category\Category;
$this->title = Yii::t('app', 'Продукты');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Создать продукт'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'id',
'format' => 'text',
'label' => Yii::t('app', 'Базовое ID'),
],
[
'attribute' => 'base_id',
'format' => 'text',
'label' => Yii::t('app', 'Экспортное ID'),
],
[
'attribute' => 'category',
'format' => 'text',
'label' => Yii::t('app', 'Категория'),
],
[
'attribute' => 'cloth',
'format' => 'text',
'label' => Yii::t('app', 'Категория ткани'),
],
[
'attribute' => 'title',
'format' => 'text',
'label' => Yii::t('app', 'Наименование продукта'),
],
[
'attribute' => 'description',
'format' => 'text',
'label' => Yii::t('app', 'Описание продукта'),
],
[
'attribute' => 'categoryName',
'label' => 'Категория',
'value' => 'category.name',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
Что не так?