多对多可以使用all()来调用
对数据提供者进行修改
/** * Lists all ArticleCate models. * @return mixed */ public function actionIndex() { $searchModel = new ArticleCateSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->setModels($this->getTree($dataProvider->getModels(), 0, 0)); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } public function getTree($data, $pid, $level = 0) { $list = array(); foreach ($data as $k => $v) { if ($v['parent_id'] == $pid) { $v['name'] = '| — —' . str_repeat('| — —', $level) . $v['name']; $list[] = $v; $list = array_merge($list, self::getTree($data, $v['id'], $level + 1)); } } return $list; }