Код: Выделить всё
<?php
/* @var $this yii\web\View */
use yii\helpers\Url;
use yii\helpers\Html;
?>
<div class="windows">
<ol class="breadcrumb na">
<li><a href="<?= Url::home()?>">Главная</a></li>
<li><? if( $category['parent_id'] == 0):?><a href="<?= Url::to(['categories/index'])?>"><?= 'Каталог категорий' ?></a>
<? else :?>
<a href="<?= Url::to(['categories/view', 'id' => $category['parent_id']])?>">
<? foreach ($ca as $cats): ?><?= $cats['name'] ?><?endforeach;?></a>
<? endif;?></li>
<li><?= $category->name ?></li>
</ol>
<h2><?= $category->name?></h2>
<h4><?= $category->short_text?></h4>
<h5><?= $category->main_text?></h5>
<? if( $category['parent_id'] == 0):?>
<div class="block">
<? foreach ($cat as $cats):?>
<? $mainImg = $cats->getImage() ?>
<div class="preview">
<a href="<?= Url::to(['categories/view', 'id' => $cats['id']])?>">
<?= Html::img($mainImg->getUrl(), ['width' => '230px']) ?></a>
<br><a href="<?= Url::to(['categories/view', 'id' => $cats['id']])?>" ><strong><?= $cats['name'] ?></strong></a>
</div>
<? endforeach; ?>
</div>
А в этом получаю ошибку:
Код: Выделить всё
<? else :?>
<div class="block">
<? foreach ($subcats as $subcat):?>
<? $mainImg = $subcat->getImage() ?>
<div class="preview"><a href="<?= Url::to(['series/view', 'id' => $subcat['id']])?>">
<?= Html::img($mainImg->getUrl(), ['width' => '230px']) ?></a>
<br><a href="<?= Url::to(['series/view', 'id' => $subcat['id']])?>" ><strong><?= $subcat['name'] ?></strong></a>
</div>
<? endforeach; ?>
</div>
<? endif;?>
</div>
Код: Выделить всё
<? $mainImg = $subcat->getImage() ?>
Код: Выделить всё
namespace app\controllers;
use app\models\Categories;
use app\models\Products;
use app\models\Series;
use yii\web\HttpException;
use Yii;
class CategoriesController extends AppController
{
public function actionIndex()
{
$cat = Categories::find()->where(['parent_id' => ''])->asArray()->all();
$this->setMeta('Каталог категорий');
return $this->render('index', compact('cat', 'category'));
}
public function actionView($id)
{
//$id = Yii::$app->request->get('id');
$category = Categories::findOne($id);
$categories = $category['parent_id'];
if (empty($category))
throw new HttpException(404, 'Такой категории нет');
$cat = Categories::find()->where(['parent_id' => $id])->all();
$ca = Categories::find()->where(['id' => $categories])->asArray()->all();
$subcats = Series::find()->where(['category_id' => $id])->with('products')->asArray()->all();
$prod = Products::find()->where(['serie_id' => $subcats])->asArray()->all();
$this->setMeta($category->name, $category->keywords, $category->description);
return $this->render('view', compact('id', 'cat', 'subcats', 'category', 'ca', 'prod', 'sub'));
}
}
Модель:
Код: Выделить всё
<?php
/**
* Created by PhpStorm.
* User: Игорь
* Date: 27.10.2016
* Time: 0:19
*/
namespace app\models;
use yii\db\ActiveRecord;
class Series extends ActiveRecord
{
public function behaviors()
{
return [
'image' => [
'class' => 'rico\yii2images\behaviors\ImageBehave',
]
];
}
static function tableName()
{
return 'series';
}
public function getCategories()
{
return $this->hasOne(Categories::className(), ['id' => 'category_id']);
}
public function getProducts()
{
return $this->hasMany(Products::className(), ['serie_id' => 'id']);
}
public function getSeriesColor()
{
return $this->hasMany(SeriesColor::className(), ['serie_id' => 'id']);
}
public function getColors()
{
return $this->hasMany(Colors::className(), ['id' => 'color_id'])->via('seriesColor');
}
public function getCountry()
{
return $this->hasOne(Country::className(), ['id' => 'countries']);
}
public function getCategoriesPrice()
{
return $this->hasOne(CategoriesPrice::className(), ['id' => 'category_price']);
}
public function getStyles()
{
return $this->hasOne(Styles::className(), ['id' => 'style']);
}
}