Yii2 Получить из базы модель, с данными из связанной таблицы и засунуть ее в griedview

Нужно получить модель product, с данными из связанной таблицы tags и выгрузить все это в gridviews, в документации/интернете/видосах на ютубе нечего полезного нету, все варианты не работают

База данных выглядит следующим образом products связана с tags через producttags, следовательно у одного продукта может быть много тегов и у одного тега может быть много продуктов введите сюда описание изображения

Модель продукта :

class Product extends ActiveRecord
{

    public static function tableName(): string
    {
        return 'products';
    }

    public function rules(): array
    {
        return [
            ['name', 'string'],
            [['name', 'price'], 'required'],
            ['price', 'integer']

        ];
    }

    public function getTags()
    {
        return $this->hasMany(Tag::class, ['id' => 'tag_id'])
            ->viaTable('productTags', ['product_id' => 'id']);
    }

Модель для поиска продукта

class ProductSearch extends Product
{
    public function rules(): array
    {
        return [
            ['name', 'string'],
            ['tags', 'string'],
            [['name', 'price'], 'safe'],
            ['price', 'integer']

        ];
    }

    public function search($params): ActiveDataProvider
    {
        $query = self::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
            'price' => $this->price,
        ]);
        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'name', $this->name]);
        return $dataProvider;
    }
}

Контроллер продукта: ` public function behaviors() { return array_merge( parent::behaviors(), [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ] ); }

public function actionIndex()
{
    $searchModel = new ProductSearch();
    $dataProvider = $searchModel->search($this->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

public function actionView($id)
{
    return $this->render('view', [
        'model' => $this->findModel($id),
    ]);

}

public function actionCreate()
{
    $model = new Product();

    if ($this->request->isPost) {
        if ($model->load($this->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
    } else {
        $model->loadDefaultValues();
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    }

    return $this->render('update', [
        'model' => $model,
    ]);
}

public function actionDelete($id)
{

    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}


protected function findModel($id)
{
    if (($model = Product::findOne(['id' => $id])) !== null) {
        return $model;
    }

    throw new NotFoundHttpException('The requested page does not exist.');
}`

Cтраница index:

$this->title = 'Продукты';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Добавить продукт', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
//            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'name',
            'price',
            'tags.name',
            [
                'class' => ActionColumn::className(),
                'urlCreator' => function ($action, Product $model, $key, $index, $column) {
                    return Url::toRoute([$action, 'id' => $model->id]);
                 }
            ],
        ],
    ]); ?>
</div>

Ответы (0 шт):