/** * Finds the Category model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Category the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @param int $pid * @param int $level * @param bool $onlyActive * @return array */ public static function getCategoryTree($pid = 0, $level = 0, $onlyActive = true) { $where['parent'] = $pid; if ($onlyActive) { $where["status"] = Category::STATUS_ACTIVE; } $data = Category::find()->where($where)->asArray()->all(); $tree = array(); $level++; if (count($data) > 0) { foreach ($data as $v) { $child = self::getCategoryTree($v['id'], $level, $onlyActive); $tree[] = array('parent' => $v, 'child' => $child, 'level' => $level); } } return $tree; }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model modules\cms\models\Category */ /* @var $form yii\widgets\ActiveForm */ $categorys = \modules\cms\models\Category::eachCategoryTree(); ?> <div class="category-form col-xs-8"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'title')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'parent')->dropDownList($categorys['data']); ?> <?php echo $form->field($model, 'status')->dropDownList([\modules\cms\models\Category::STATUS_ACTIVE => "显示", \modules\cms\models\Category::STATUS_DELETED => "隐藏"]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? '保存' : '保存', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
public function getCategoryModel() { return Category::findOne($this->category_id); }
<table id="simple-table" class="table table-striped table-bordered table-hover"> <thead> <tr> <th class="center"> <label class="pos-rel"> <!-- <input type="checkbox" class="ace">--> <span class="lbl">ID</span> </label> </th> <th>标题</th> <th> <select id="category" name="category"> <option value="">All</option> <?php foreach (\modules\cms\models\Category::eachCategoryTree()['data'] as $key => $item) { ?> <option value="<?php echo $key; ?> "><?php echo $item; ?> </option> <?php } ?> </select> <script> $(document).ready(function () { var categoryId = '<?php