Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionCategoryTree()
- {
- $categories = CategoryTree::find()
- ->joinWith('lang')
- ->andWhere(['parent_id' => 1])
- ->all();
- $categoryTree = [];
- foreach ($categories as $category) {
- /**
- * @var $category CategoryTree
- */
- $children = $category->children()->joinWith('lang')->all();
- $childrens = [];
- if (!empty($children) && is_array($children)) {
- foreach ($children as $categoryChildren) {
- $childrens[] = [
- 'id' => $categoryChildren->id,
- 'text' => $categoryChildren->lang->name
- ];
- }
- } else {
- $children = CategoryTree::find()
- ->joinWith('lang')
- ->andWhere(['parent_id' => $category->id])
- ->orderBy('id')
- ->all();
- if (!empty($children) && is_array($children)) {
- foreach ($children as $categoryChildren) {
- $childrens[] = [
- 'id' => $categoryChildren->id,
- 'text' => $categoryChildren->lang->name
- ];
- }
- }
- }
- $categoryTree[] = [
- 'id' => $category->id,
- 'text' => $category->lang->name,
- 'children' => $childrens
- ];
- }
- Yii::$app->response->format = Response::FORMAT_JSON;
- return $categoryTree;
- }
Add Comment
Please, Sign In to add comment