public function listAction(){
S([
'type' => 'memcache',
'host' => '127.0.0.1',
'port' => '11211'
]);
$cat_tree = S('cat_tree');
if (false === $cat_tree){
echo 'non-cache';
$m_cat = D('Cat');
$cat_tree = $m_cat->getTreeList();
S('cat_tree', $cat_tree);
}
$this->assign('list', $cat_tree, PHP_INT_MAX);
$this->display();
}
public function deleteAction($cat_id){
$m_cat = D('Cat');
if ($m_cat->delete($cat_id)){
S([
'type' => 'memcache',
'host' => '127.0.0.1',
'port' => '11211'
]);
S('cat_tree', NULL);
$this->success('删除成功', U('Back/Cat/list'), 0);
} else {
$this->error('删除失败', U('Back/Cat/list'));
}
}
class CatModel extends \Think\Model {
protected $tablename = 'category';
public function getTreeList(){
$list = $this->order('sort_order desc')->select();
return $this->_getTree($list, 0, 0);
}
protected function _getTree($rows, $p_id=0, $deep=0){
static $tree = [];
foreach($rows as $row){
if($row['parent_id'] == $p_id){
$row['deep'] = $deep;
$tree[] = $row;
$this->_getTree($rows, $row['cat_id'], 1+$deep);
}
}
}
}