public function onMenuConfig($event) { $collection = Taxonomy::find()->sort()->asArray()->all(); $event->parameters->set($this->id, ["name" => "分类目录", "id" => $this->id, "tree" => NestedSetsTree::generateTree($collection, function ($item) { $item["id"] = $item['taxonomy_id']; // 这里还是用taxonomy_id的好..不用slug,因为slug很容易被更改 return $item; })]); }
<?php echo $form->field($model, 'slug'); ?> <div class="form-group field-category-title required"> <label for="category-parent" class="control-label"><?php echo Yii::t('hass', 'Parent category'); ?> </label> <select class="form-control" id="category-parent" name="parent"> <option value="" class="smooth"><?php echo Yii::t('hass', 'No'); ?> </option> <?php foreach (Taxonomy::find()->sort()->asArray()->all() as $node) { ?> <option value="<?php echo $node['taxonomy_id']; ?> " <?php if ($parentId == $node['taxonomy_id']) { echo 'SELECTED'; } ?> ><?php echo str_repeat(" ", $node['depth']) . " " . $node['name']; ?> </option>
/** * Get cached tree structure of category objects * * @return array */ public function getTaxonomytree() { if ($this->root != null) { $countries = Taxonomy::findOne(['slug' => $this->root]); $collection = $countries->children()->asArray()->all(); } else { $collection = Taxonomy::find()->sort()->asArray()->all(); } $indexs = TaxonomyIndex::find()->where(["entity_id" => $this->getEntityId(), "entity" => $this->getEntityClass()])->asArray()->all(); $indexs = ArrayHelper::getColumn($indexs, "taxonomy_id"); $trees = NestedSetsTree::generateTree($collection, function ($item) use($indexs) { $item["checked"] = false; if (in_array($item["taxonomy_id"], $indexs)) { $item["checked"] = true; } return $item; }); return $trees; }