public function beforeSave($insert) { if (parent::beforeSave($insert)) { $model = new AssignRole(); if ($this->parent_id == NULL) { $this->parents = $this->id . '-'; $model->save(); } else { $pparents = $model->findOne($this->parent_id)->parents; $this->parents = $pparents . $this->id . '-'; $model->save(); } return true; } return false; }
/** * Finds the AssignRole model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return AssignRole the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AssignRole::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use common\widgets\Recursive; use app\models\AssignRole; use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $model app\models\AssignRole */ /* @var $form yii\widgets\ActiveForm */ $groups = AssignRole::findAll(['state' => 1]); function cmp($a, $b) { return strcmp($a->parents, $b->parents); } usort($groups, "cmp"); $data = Recursive::sortArrayDropDown($groups); ?> <div class="row-fluid"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'title')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'parent_id')->dropdownList($data, ['prompt' => 'Nhóm gốc']);