На данный момент вывожу просто все бренды вот так:
Код: Выделить всё
$brands = Brands::find()->all();
Я понимаю что мне нужен запрос в виде, но как получить ID этих брендов ни пойму!
$brands = Brands::findAll([1, 2, 3, 4]);
Update:
Мой CatController:
Код: Выделить всё
public function actionView($id)
{
$id = Yii::$app->request->get('id');
$category = Cat::find()->where(['id' => $id])->one();
if(!$category){
throw new NotFoundHttpException('Такой категории не существует');
}
$prod = Prod::find()->where(['category_id' => $id])->all();
$brnd = Brnd::find()->all(); --> тут вывожу все бренды
return $this->render('view', [
'model' => $this->findModel($id),
'prod' => $prod,
'category' => $category,
'brnd' => $brnd,
]);
}
Код: Выделить всё
<div class="lists" style="display: flex;">
<?php foreach ($brnd as $b): ?>
<div class="item" style="border: 1px solid #aaa; padding:10px">
<?= Html::a($b->name, ['brnd/view', 'id' => $b->id], ['class' => 'class-name']) ?>
</div>
<?php endforeach; ?>
</div>
Код: Выделить всё
<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "cat".
*
* @property int $id
* @property int $parent_id
* @property string $name
*/
class Cat extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'cat';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['parent_id', 'name'], 'required'],
[['parent_id'], 'integer'],
[['name'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'parent_id' => 'Parent ID',
'name' => 'Name',
];
}
}
Код: Выделить всё
<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "prod".
*
* @property int $id
* @property int $category_id
* @property int $brand_id
* @property string $name
*/
class Prod extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'prod';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['category_id', 'brand_id', 'name'], 'required'],
[['category_id', 'brand_id'], 'integer'],
[['name'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'category_id' => 'Category ID',
'brand_id' => 'Brand ID',
'name' => 'Name',
];
}
// Связываем категории (получаем имя категории)
public function getCat()
{
return $this->hasOne(Cat::className(), ['id' => 'category_id']);
}
// Связываем бренды (получаем имя бренда)
public function getBrnd()
{
return $this->hasOne(Brnd::className(), ['id' => 'brand_id']);
}
}
Код: Выделить всё
<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "brnd".
*
* @property int $id
* @property string $name
*/
class Brnd extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'brnd';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'required'],
[['name'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
];
}
}