Извиняюсь за свою тупость, но все же прошу еще немного пояснить. Насколько я понял алгоритм дальнейших действий следующий:
- В модели Categories я создаю метод:
Код: Выделить всё
public function getSubcats()
{
return $this->hasMany(Subcats::className(), ['parent_id' => '?']);
}
Далее создаю новую модель Subcats, в ней соответственно создаю метод:
Код: Выделить всё
class Products extends ActiveRecord
{
public function getCategories()
{
return $this->hasOne(Categories::className(), ['?' => 'parent_id']);
}
}
- В контроллере
Код: Выделить всё
public function actionIndex()
{
$cat = Categories::find()->where(['parent_id' => ''])->limit(9)->all();
$subcat = Categories::find()->where(['parent_id' => ''])->all();
$counts = Products::find()->where(['count'=> '1'])->limit(3)->all();
return $this->render('index', compact('cat','subcat','counts'));
/*$counts = Products::find()->where(['count'=> '1'])->limit(3)->all();
return $this->render('index', compact('counts'));*/
}
- В виде
Код: Выделить всё
<? if( !empty($cat)):?>
<div class="block">
<? foreach ($cat as $cats):?>
<div class="preview"><a href="#"><?= Html::img("@web/pic/{$cats->category_img}", ['alt' => $cats->name]) ?></a>
<br><a href="#" ><strong><?= $cats->name ?></strong></a>
<ul>
<? foreach ($cats->subcat as $subcats) :?>
<li><a href="#"><?= $subcats->name ?></a></li>
<? endforeach; ?>
</ul>
</div>
<? endforeach; ?>
</div>
<? endif;?>
Вопрос в том, что таблицы Subcats у меня нет и соответственно полей, которые мне необходимы связать с полем parent_id в таблице Categories тоже нет.
Категории и подкатегории разделяются в таблице Categories полем parent_id т.е. Категории 10 шт - 'id' ->'1-10', 'parent_id' -> '0', подкатегории 'id' ->'11-44' 'parent_id' -> '1-10'. В результате получаю сообщение об ошибке:
Код: Выделить всё
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: app\models\Categories::subcat
1. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\Component.php at line 143
134135136137138139140141142143144145146147148149150151152
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
}
/**
* Sets the value of a component property.
* This method will check in the following order and act accordingly:
*
* - a property defined by a setter: set the property value
* - an event in the format of "on xyz": attach the handler to the event "xyz"
2. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\db\BaseActiveRecord.php at line 252 – yii\base\Component::__get('subcat')
3. in D:\OpenServer\domains\mebel.local\views\categories\index.php at line 35 – yii\db\BaseActiveRecord::__get('subcat')
29303132333435363738394041
<? foreach ($cat as $cats):?>
<div class="preview"><a href="#"><?= Html::img("@web/pic/{$cats->category_img}", ['alt' => $cats->name]) ?></a>
<br><a href="#" ><strong><?= $cats->name ?></strong></a>
<ul>
<? foreach ($cats->subcat as $subcats) :?>
<li><a href="#"><?= $subcats->name ?></a></li>
<? endforeach; ?>
</ul>
</div>
<? endforeach; ?>
4. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\View.php at line 325 – require('D:\OpenServer\domains\mebel.loca...')
5. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\View.php at line 247 – yii\base\View::renderPhpFile('D:\OpenServer\domains\mebel.loca...', ['cat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories, ...], 'subcat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories], 'counts' => [app\models\Products, app\models\Products, app\models\Products]])
6. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\View.php at line 149 – yii\base\View::renderFile('D:\OpenServer\domains\mebel.loca...', ['cat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories, ...], 'subcat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories], 'counts' => [app\models\Products, app\models\Products, app\models\Products]], app\controllers\CategoriesController)
7. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\Controller.php at line 378 – yii\base\View::render('index', ['cat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories, ...], 'subcat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories], 'counts' => [app\models\Products, app\models\Products, app\models\Products]], app\controllers\CategoriesController)
8. in D:\OpenServer\domains\mebel.local\controllers\CategoriesController.php at line 24 – yii\base\Controller::render('index', ['cat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories, ...], 'subcat' => [app\models\Categories, app\models\Categories, app\models\Categories, app\models\Categories], 'counts' => [app\models\Products, app\models\Products, app\models\Products]])
18192021222324252627282930
public function actionIndex()
{
$cat = Categories::find()->where(['parent_id' => ''])->limit(9)->all();
$subcat = Categories::find()->where(['parent_id' => '1'])->all();
$counts = Products::find()->where(['count'=> '1'])->limit(3)->all();
return $this->render('index', compact('cat','subcat','counts'));
/*$counts = Products::find()->where(['count'=> '1'])->limit(3)->all();
return $this->render('index', compact('counts'));*/
}
public function actionView($id)
9. app\controllers\CategoriesController::actionIndex()
10. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\InlineAction.php at line 55 – call_user_func_array([app\controllers\CategoriesController, 'actionIndex'], [])
11. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\Controller.php at line 154 – yii\base\InlineAction::runWithParams([])
12. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\Module.php at line 454 – yii\base\Controller::runAction('index', [])
13. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\web\Application.php at line 87 – yii\base\Module::runAction('', [])
14. in D:\OpenServer\domains\mebel.local\vendor\yiisoft\yii2\base\Application.php at line 375 – yii\web\Application::handleRequest(yii\web\Request)
15. in D:\OpenServer\domains\mebel.local\web\index.php at line 13 – yii\base\Application::run()
78910111213
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
require_once __DIR__ . '/../functions.php';
(new yii\web\Application($config))->run();
$_COOKIE = [
'_ym_uid' => '1473366303873874295',
'__utma' => '97770499.2012353620.1473366303.1473887075.1473927775.14',
'__utmz' => '97770499.1473366303.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
'_ym_isad' => '2',
'_csrf' => 'aa663cae4c92f4fa0d6e8942ea884ccaf4ae165bf11c6a9ff1523b60997dc890a:2:{i:0;s:5:"_csrf";i:1;s:32:"Fu3lX_plr6duGYqI5Hh913vuVOivALjC";}',
'__utmb' => '97770499.2.10.1473927775',
'__utmc' => '97770499',
'__utmt' => '1',
];