SHARE
TWEET

Untitled

a guest Dec 17th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function actionCategoryTree()
  2. {
  3.     $categories = CategoryTree::find()
  4.         ->joinWith('lang')
  5.         ->andWhere(['parent_id' => 1])
  6.         ->all();
  7.     $categoryTree = [];
  8.     foreach ($categories as $category) {
  9.         /**
  10.         * @var $category CategoryTree
  11.         */
  12.         $children = $category->children()->joinWith('lang')->all();
  13.         $childrens = [];
  14.         if (!empty($children) && is_array($children)) {
  15.             foreach ($children as $categoryChildren) {
  16.                 $childrens[] = [
  17.                     'id' => $categoryChildren->id,
  18.                     'text' => $categoryChildren->lang->name
  19.                 ];
  20.             }
  21.         } else {
  22.             $children = CategoryTree::find()
  23.                 ->joinWith('lang')
  24.                 ->andWhere(['parent_id' => $category->id])
  25.                 ->orderBy('id')
  26.                 ->all();
  27.             if (!empty($children) && is_array($children)) {
  28.                 foreach ($children as $categoryChildren) {
  29.                     $childrens[] = [
  30.                         'id' => $categoryChildren->id,
  31.                         'text' => $categoryChildren->lang->name
  32.                     ];
  33.                 }
  34.             }
  35.         }
  36.         $categoryTree[] = [
  37.             'id' => $category->id,
  38.             'text' => $category->lang->name,
  39.             'children' => $childrens
  40.         ];
  41.     }
  42.    
  43.     Yii::$app->response->format = Response::FORMAT_JSON;
  44.     return $categoryTree;
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top