数据表是设计 包含 id name pid 三者 就可以了
第一步: controller:
public function actionFunctionPoint() { $items = FunctionPoint::getFunctionPoint(); return $this->render('function-point', [ 'items' => $items, ]); }
第二部 model操作数据库
/** * 获得所有系统功能 * @return array */ public static function getFunctionPoint() { $data = []; self::_getFunctionPointList($data,0,1); return $data; } /**根据父类元素 查询所有子类原生 * @param $parentId pid * @return array */ public static function _getFunctionPointList(&$data,$parentId,$level) { $sql = 'SELECT * FROM t_boss_function_point WHERE is_deleted =0 AND parent_id =:parentId'; $cmd = \Yii::$app->db->createCommand($sql); $cmd->bindValue(':parentId', $parentId); $results = $cmd->queryAll(); foreach ($results as $result) { $result['level'] = $level; array_push($data,$result); self::_getFunctionPointList($data,$result['id'],$level+1); } }
第三步 :view视图的展示
功能点二 递归删除父节点下面的所有子节点
控制器
public function actionFunctionPointDeleteHandler() { $request = Yii::$app->request; $id = $request->post('id'); $ids = FunctionPoint::getAllChildIds($id); $ids = $id.$ids; $result= FunctionPoint::deleteFunctionPoint($ids); return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败'); }
/ * 删除系统功能点 * @param $ids * @return bool */ public static function deleteFunctionPoint($ids) {// $cmd = \Yii::$app->db->createCommand('UPDATE t_boss_function_point SET is_deleted = 1 WHERE FIND_IN_SET(id,:ids);'); $cmd = \Yii::$app->db->createCommand('CALL p_boss_function_point_delete(:ids);'); $cmd->bindValue(':ids', $ids); $result = $cmd->execute(); return $result > 0; } /** * 获得所有的子节点 的id值 * @param $parentId * @return string 返回id字符串 */ public static function getAllChildIds($parentId) { $ids =''; $sql = 'SELECT `id` FROM t_boss_function_point WHERE is_deleted =0 AND parent_id =:parentId'; $cmd = \Yii::$app->db->createCommand($sql); $cmd->bindValue(':parentId', $parentId); $results = $cmd->queryAll(); foreach($results as $v){ $ids .=",".$v['id']; $ids .= self::getAllChildIds($v['id']); } return $ids; }}