Код: Выделить всё
return $this->hasMany(Products::className(), ['id' => 'id_prod']);
Код: Выделить всё
viaTable(ProductsImage::tableName(), ['product_id' => id])
массив получается такой:
Код: Выделить всё
Array
(
[0] => Array
(
[id] => 1
[id_cat] => 1
[id_subcat] => 0
[id_rub] => 15
[id_prod] => 1
[category] => Array
(
)
)
Код: Выделить всё
SELECT * FROM `products` WHERE 0=1
сам код:
Код: Выделить всё
<?php
namespace app\models;
use yii\db\ActiveRecord;
class ProductsImage extends ActiveRecord {
public static function tableName() {
return ('products_image');
}
}
?>
Код: Выделить всё
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Products extends ActiveRecord {
public static function tableName() {
return ('products');
}
public function getProducts() {
return $this->hasMany(ProductsImage::className(), ['product_id' => id]);
//$query = $this->hasMany(ProductsImage::className(), ['product_id' => id]->viaTable(Categories::tableName(), ['id' => 'category_id']));
}
}
?>
Код: Выделить всё
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Category extends ActiveRecord {
public static function tableName() {
return ('categories');
}
public function getCategory() {
return $this->hasMany(Products::className(), ['id' => 'id_prod'])->viaTable(ProductsImage::tableName(), ['product_id' => id]);
}
}
?>
Код: Выделить всё
<?php
namespace app\controllers;
use Yii;
use app\models\Pages;
use app\models\Sidebar;
use app\models\Products;
use app\models\ProductsImage;
use app\models\Category;
class ContentController extends AppController {
public function actionProducts() {
$param = Yii::$app->request->get();
if (isset($param['cat']) || !empty($param['cat'])) {
$param = $param['cat'];
}
$model = new Sidebar();
$cat = Sidebar::find()->asArray()->where("name_en = '$param'")->all();
$id = $cat[0]['id'];
$model = new Category();
$result = Category::find()->with('category')->asArray()->where("id_cat = $id")->all();
debug($result);
return $this->render('products', compact('query', 'result'));
}
}