sql:
控制器:
代码语言:javascript复制<?php
namespace frontendcontrollers;
use appmodelsShopCategory;
use yiiwebController;
use Yii;
class CygController extends Controller
{
public function actionList()
{
$model = new ShopCategory;
//获取列表
$cates = $model->getTreeList();
return $this->render("cates", ['cates' => $cates]);
}
public function actionAdd()
{
$model = new ShopCategory();
$list = $model->getOptions();
if (Yii::$app->request->isPost) {
$post = Yii::$app->request->post();
if ($model->add($post)) {
Yii::$app->session->setFlash("info", "添加成功");
}
}
return $this->render("add", ['list' => $list, 'model' => $model]);
}
public function actionMod()
{
$cateid = Yii::$app->request->get("cateid");
$model = ShopCategory::find()->where('cateid = :id', [':id' => $cateid])->one();
if (Yii::$app->request->isPost) {
$post = Yii::$app->request->post();
if ($model->load($post) && $model->save()) {
Yii::$app->session->setFlash('info', '修改成功');
}
}
$list = $model->getOptions();
return $this->render('add', ['model' => $model, 'list' => $list]);
}
public function actionDel()
{
try {
$cateid = Yii::$app->request->get('cateid');
if (empty($cateid)) {
throw new Exception('参数错误');
}
$data = ShopCategory::find()->where('parentid = :pid', [":pid" => $cateid])->one();
if ($data) {
throw new Exception('该分类下有子类,不允许删除');
}
if (!ShopCategory::deleteAll('cateid = :id', [":id" => $cateid])) {
throw new Exception('删除失败');
}
} catch(Exception $e) {
Yii::$app->session->setFlash('info', $e->getMessage());
}
return $this->redirect(['cyg/list']);
}
}
model:
代码语言:javascript复制<?php
namespace appmodels;
use yiidbActiveRecord;
use Yii;
use yiihelpersArrayHelper;
/**
* This is the model class for table "shop_category".
*
* @property string $cateid
* @property string $title
* @property string $parentid
* @property int $createtime
*/
class ShopCategory extends yiidbActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'shop_category';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['parentid', 'createtime'], 'integer'],
[['title'], 'string', 'max' => 32],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'cateid' => 'Cateid',
'title' => 'Title',
'parentid' => 'Parentid',
'createtime' => 'Createtime',
];
}
public function add($data)
{
$data['ShopCategory']['createtime'] = time();
if ($this->load($data) && $this->save()) {
return true;
}
return false;
}
public function getData()
{//获取sopcategory类的数据
$cates = self::find()->all();
//列成数组
$cates = ArrayHelper::toArray($cates);
return $cates;//返回
}
public function getTree($cates, $pid = 0)//获取树的子类
{
$tree = [];
foreach($cates as $cate) {
if ($cate['parentid'] == $pid) {//如果是顶级分类的话.
$tree[] = $cate;//就把顶级分类放到里面,以次内推.....
$tree = array_merge($tree, $this->getTree($cates, $cate['cateid']));//继续递归调用自己,意思是
//比如新闻
}
}
return $tree;
}
public function setPrefix($data, $p = "|-----")
{
var_dump($data);
$tree = [];
$num = 1;
$prefix = [0 => 1];
while($val = current($data)) {
$key = key($data);//key开始在0的位置
if ($key > 0) {//从第二个开始
if ($data[$key - 1]['parentid'] != $val['parentid']) {
$num ;
}
}
if (array_key_exists($val['parentid'], $prefix)) {
$num = $prefix[$val['parentid']];
}
$val['title'] = str_repeat($p, $num).$val['title'];
$prefix[$val['parentid']] = $num;
$tree[] = $val;
next($data);
}
return $tree;
}
public function getOptions()
{
$data = $this->getData();
$tree = $this->getTree($data);
$tree = $this->setPrefix($tree);
$options = ['添加顶级分类'];
foreach($tree as $cate) {
$options[$cate['cateid']] = $cate['title'];
}
return $options;
}
public function getTreeList()
{
$data = $this->getData();
$tree = $this->getTree($data);
return $tree = $this->setPrefix($tree);
}
public static function getMenu()
{
$top = self::find()->where('parentid = :pid', [":pid" => 0])->limit(11)->orderby('createtime asc')->asArray()->all();
$data = [];
foreach((array)$top as $k=>$cate) {
$cate['children'] = self::find()->where("parentid = :pid", [":pid" => $cate['cateid']])->limit(10)->asArray()->all();
$data[$k] = $cate;
}
return $data;
}
}
视图:add.php
代码语言:javascript复制<?php
use yiibootstrapActiveForm;
use yiihelpersHtml;
?>
<link rel="stylesheet" href="assets/admin/css/compiled/new-user.css" type="text/css" media="screen" />
<!-- main container -->
<div class="content">
<div class="container-fluid">
<div id="pad-wrapper" class="new-user">
<div class="row-fluid header">
<h3>添加新分类</h3>
</div>
<div class="row-fluid form-wrapper">
<!-- left column -->
<div class="span9 with-sidebar">
<div class="container">
<?php
if (Yii::$app->session->hasFlash('info')) {
echo Yii::$app->session->getFlash('info');
}
$form = ActiveForm::begin([
'fieldConfig' => [
'template' => '<div class="span12 field-box">{label}{input}</div>{error}',
],
'options' => [
'class' => 'new_user_form inline-input',
],
]);
echo $form->field($model, 'parentid')->dropDownList($list);
echo $form->field($model, 'title')->textInput(['class' => 'span9']);
?>
<div class="span11 field-box actions">
<?php echo Html::submitButton('添加', ['class' => 'btn-glow primary']);?>
<span>OR</span>
<?php echo Html::resetButton('取消', ['class' => 'reset']); ?>
<?php echo Html::a('返回', ['cyg/list'], ['class' => 'btn btn-primary fanhui']);?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
<!-- side right column -->
</div>
</div>
</div>
</div>
<!-- end main container -->
cates.php
代码语言:javascript复制 <link rel="stylesheet" href="assets/admin/css/compiled/user-list.css" type="text/css" media="screen" />
<!-- main container -->
<div class="content">
<div class="container-fluid">
<div id="pad-wrapper" class="users-list">
<div class="row-fluid header">
<h3>分类列表</h3>
<div class="span10 pull-right">
<a href="<?php echo yiihelpersUrl::to(['cyg/add']) ?>" class="btn-flat success pull-right">
<span>+</span>
添加新分类
</a>
</div>
</div>
<?php
if (Yii::$app->session->hasFlash('info')) {
echo Yii::$app->session->getFlash('info');
}
?>
<!-- Users table -->
<div class="row-fluid table">
<table class="table table-hover">
<thead>
<tr>
<th class="span3 sortable">
<span class="line"></span>分类ID
</th>
<th class="span3 sortable">
<span class="line"></span>分类名称
</th>
<th class="span3 sortable align-right">
<span class="line"></span>操作
</th>
</tr>
</thead>
<tbody>
<!-- row -->
<?php foreach($cates as $cate): ?>
<tr class="first">
<td>
<?php echo $cate['cateid'] ?>
</td>
<td>
<?php echo $cate['title'] ; ?>
</td>
<td class="align-right">
<a href="<?php echo yiihelpersUrl::to(['cyg/mod', 'cateid' => $cate['cateid']]); ?>">编辑</a>
<a href="<?php echo yiihelpersUrl::to(['cyg/del', 'cateid' => $cate['cateid']]); ?>">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="pagination pull-right">
<?php /*echo yiiwidgetsLinkPager::widget([
'pagination' => $pager,
'prevPageLabel' => '‹',
'nextPageLabel' => '›',
]);*/ ?>
</div>
<!-- end users table -->
</div>
</div>
</div>
<!-- end main container -->
结构:
效果图