<?php
namespace frontend\libs;
use Yii;
use yii\base\Action;
use yii\base\ActionFilter;
use backend\models\Webinfo;
use backend\models\CateCategory;
use backend\models\CasesCate;
class LayoutsFilter extends ActionFilter{
// public $a_name;
// public $a_id;
//action之前运行
public function beforeAction($action)
{
// $model = CateCategory::find()->where(['father'=>1,'status'=>1])->orderBy("orders DESC")->all();
// $model1 = CateCategory::find()->where(['father'=>2,'status'=>1])->orderBy("orders DESC")->all();
// $model2 = CateCategory::find()->where(['father'=>3,'status'=>1])->orderBy("orders DESC")->all();
// $return=[$model,$model1,$model2];
// $model = CateCategory::find()->where(['cate_category.status'=>1])
// ->rightJoin('cases_cate','`cate_category`.`father` = `cases_cate`.`id`')
// ->orderBy("cate_category.orders DESC")->all();
// $model = CateCategory::find()
// ->select('cate_category.*,cases_cate.name aname')
// ->leftJoin('cases_cate','`cate_category`.`father` = `cases_cate`.`id`')
// ->all();
$model = CateCategory::find()->select(['cate_category.*','cases_cate.name as a_name','cases_cate.id as a_id'])
->where(['cate_category.status'=>1])
->orderBy("cate_category.orders DESC")
->innerjoin('cases_cate', 'cases_cate.id=cate_category.father')
->asArray()
->all();
$model=$this->easea($model,1);
$return=$model;
//向layout输出网站信息
$webinfo = Webinfo::find()->one();
$view = Yii::$app->view;
$view->params['webinfo'] = $webinfo;
$view->params['cate']=$return;
return true;
//如果返回值为false,则action不会运行,如果返回值为true,则action运行
}
//action之后运行
public function afterAction($action, $result)
{
//可以对action输出的$result进行过滤,return的内容直接显示
return $result;
}
public function easea($cates,$pid)
{
static $tree=array();
foreach($cates as $cate) {
if ($cate['father'] == $cate['a_id']) {
// $pid++;
$tree[$cate['a_name']][] = $cate;
//第二层将顶级分类放进数组,然后再次调用方法,获取当前id下的分类
// $tree = array_merge($tree, $this->easea($cates, $pid));
// $this->easea($cates, $cate['a_id']);
}
}
return $tree;
}
}
yii向网站模板输出数据库信息,和联表查询以及无限分类
最新推荐文章于 2024-09-05 11:13:05 发布