在数据透视表中查找并存储层次结构中类别位置的地址

时间:2015-08-28 15:35:28

标签: recursion laravel-5.1 php-5.6 laravel-seeding

首先,我使用的是Laravel 5.1(PHP框架),我正在运行PHP 5.6.4,我遇到的问题与递归有关。

目前我的种子文件是(递归函数的借口很差):

    <?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Migrations\Migration;
use eCommerce\Category;

class CategoryCategorySeeder extends Seeder
{

   protected $addressHolder;

   public function __construct() {
      $this->addressHolder = array();
   }

   private function recursive($array, $depth=0) {
      echo $depth."\n";
      foreach($array as $category){

         // print_r( $category['category_id'] );

         if( isset( $category['sub_categories'] ) ){            
            $this->recursive($category['sub_categories'], $depth++);
         } else {

         }
      }
   }

    public function run()
    {
        DB::table('category_category')->delete();
        $categories = [

            [
               'category_id' => 2,
               'sub_categories' => [
                  [
                     'category_id' => 3,
                     'sub_categories' => [
                        [
                           'category_id' => 6
                        ],[
                           'category_id' => 7
                        ],[
                           'category_id' => 8
                        ],[
                           'category_id' => 9
                        ],[
                           'category_id' => 10
                        ],[
                           'category_id' => 11
                        ],[
                           'category_id' => 12
                        ],[
                           'category_id' => 13
                        ],[
                           'category_id' => 14
                        ],[
                           'category_id' => 15
                        ]
                     ]
                  ],[
                     'category_id' => 4,
                     'sub_categories' => [
                        [
                           'category_id' => 18
                        ],[
                           'category_id' => 19
                        ],[
                           'category_id' => 20
                        ],[
                           'category_id' => 21
                        ],[
                           'category_id' => 22
                        ],[
                           'category_id' => 23
                        ],[
                           'category_id' => 24
                        ],[
                           'category_id' => 25
                        ],[
                           'category_id' => 26
                        ],[
                           'category_id' => 27
                        ],[
                           'category_id' => 28
                        ],[
                           'category_id' => 29
                        ],[
                           'category_id' => 30
                        ],[
                           'category_id' => 31
                        ],[
                           'category_id' => 32
                        ]
                     ]
                  ],[
                     'category_id' => 5,
                     'sub_categories' => [
                        [
                           'category_id' => 16
                        ],[
                           'category_id' => 17
                        ]
                     ]
                  ]
               ]
            ],[
               'category_id' => 33,
               'sub_categories' => [
                  [
                     'category_id' => 34
                  ],[
                     'category_id' => 35
                  ],[
                     'category_id' => 36,
                     'sub_categories' => [
                        [
                           'category_id' => 37
                        ],[
                           'category_id' => 38
                        ],[
                           'category_id' => 39
                        ],[
                           'category_id' => 40
                        ],[
                           'category_id' => 41
                        ],[
                           'category_id' => 42
                        ],[
                           'category_id' => 43
                        ],[
                           'category_id' => 44
                        ],[
                           'category_id' => 45
                        ]
                     ]
                  ]
               ]
            ],[
               'category_id' => 46,
               'sub_categories' => [
                  [
                     'category_id' => 50
                  ],[
                     'category_id' => 51,
                     'sub_categories' => [
                        [
                           'category_id' => 54
                        ],[
                           'category_id' => 55
                        ],[
                           'category_id' => 56
                        ],[
                           'category_id' => 57
                        ]
                     ]                     
                  ],[
                     'category_id' => 52
                  ],[
                     'category_id' => 53
                  ],[
                     'category_id' => 137
                  ]
               ]
            ],[
               'category_id' => 47,
               'sub_categories' => [
                  [
                     'category_id' => 58
                  ],[
                     'category_id' => 59
                  ],[
                     'category_id' => 60
                  ],[
                     'category_id' => 61
                  ],[
                     'category_id' => 62
                  ],[
                     'category_id' => 63
                  ],[
                     'category_id' => 64
                  ],[
                     'category_id' => 65
                  ],[
                     'category_id' => 66
                  ],[
                     'category_id' => 67
                  ],[
                     'category_id' => 68
                  ],[
                     'category_id' => 69
                  ],[
                     'category_id' => 70
                  ],[
                     'category_id' => 71
                  ],[
                     'category_id' => 72
                  ]
               ]
            ],[
               'category_id' => 48,
               'sub_categories' => [
                  [
                     'category_id' => 73,
                     'sub_categories' => [
                        [
                           'category_id' => 78
                        ],[
                           'category_id' => 79
                        ]
                     ]
                  ],[
                     'category_id' => 74,
                     'sub_categories' => [
                        [
                           'category_id' => 80
                        ],[
                           'category_id' => 81
                        ],[
                           'category_id' => 82
                        ],[
                           'category_id' => 83
                        ]
                     ]
                  ],[
                     'category_id' => 75,
                     'sub_categories' => [
                        [
                           'category_id' => 84
                        ],
                        [
                           'category_id' => 85
                        ],
                        [
                           'category_id' => 86
                        ]
                     ]
                  ],[
                     'category_id' => 76,
                     'sub_categories' => [
                        [
                           'category_id' => 87
                        ],[
                           'category_id' => 88,
                           'sub_categories' => [
                              [
                                 'category_id' => 93
                              ],[
                                 'category_id' => 94
                              ],[
                                 'category_id' => 95
                              ]
                           ]
                        ],[
                           'category_id' => 89
                        ],[
                           'category_id' => 90
                        ],[
                           'category_id' => 91
                        ],[
                           'category_id' => 92
                        ]
                     ]
                  ],[
                     'category_id' => 77
                  ]
               ]
            ],[
               'category_id' => 49,
               'sub_categories' => [
                  [
                     'category_id' => 96
                  ],[
                     'category_id' => 97
                  ],[
                     'category_id' => 98
                  ],[
                     'category_id' => 99
                  ],[
                     'category_id' => 100
                  ],[
                     'category_id' => 135
                  ],[
                     'category_id' => 136
                  ],[
                     'category_id' => 138
                  ],[
                     'category_id' => 139
                  ],[
                     'category_id' => 140
                  ]
               ]
            ],[
               'category_id' => 134,
               'sub_categories' => [
                  [
                     'category_id' => 101,
                     'sub_categories' => [
                        [
                           'category_id' => 108
                        ],[
                           'category_id' => 109
                        ],[
                           'category_id' => 110
                        ]
                     ]
                  ],[
                     'category_id' => 102,
                     'sub_categories' => [
                        [
                           'category_id' => 111
                        ],[
                           'category_id' => 112
                        ],[
                           'category_id' => 113
                        ]
                     ]                     
                  ],[
                     'category_id' => 103,
                     'sub_categories' => [
                        [
                           'category_id' => 114
                        ],[
                           'category_id' => 115
                        ],[
                           'category_id' => 116
                        ],[
                           'category_id' => 117
                        ],[
                           'category_id' => 118
                        ]
                     ]
                  ],[
                     'category_id' => 104
                  ],[
                     'category_id' => 105
                  ],[
                     'category_id' => 106,
                     'sub_categories' => [
                        [
                           'category_id' => 119
                        ],[
                           'category_id' => 120
                        ],[
                           'category_id' => 121
                        ],[
                           'category_id' => 122
                        ],[
                           'category_id' => 123
                        ],[
                           'category_id' => 124
                        ],[
                           'category_id' => 125
                        ],[
                           'category_id' => 126
                        ],[
                           'category_id' => 127
                        ]
                     ]
                  ],[
                     'category_id' => 107
                  ]
               ]
            ],[
               'category_id' => 128,
               'sub_categories' => [
                  [
                     'category_id' => 129
                  ],[
                     'category_id' => 130
                  ],[
                     'category_id' => 131
                  ],[
                     'category_id' => 132
                  ],[
                     'category_id' => 133
                  ]
               ]
            ],[
               'category_id' => 134
            ],[
               'category_id' => 141,
               'sub_categories' => [
                  [
                     'category_id' => 142
                  ]
               ]
            ]

        ];

        $this->recursive($categories);
      }
}

我想以递归方式遍历每个“类别”(大型多维数组)并制作一个“地址”,这是该类别来自类别层次结构的地方,这是因为在我的网站上一个类别可以属于到许多其他类别。

此种子类,如果对于多对多关系类别的数据透视表有。

在数据透视表中,我有:

  • category_id - 整数
  • parent_category_id - 整数
  • 地址 - 字符串

所以可以是一个例子(代码中的数组也是如此):

  • 6
  • 3
  • “2-3-6”

所以“2-3-6”是该类别6的特定实例的层次结构中的完整“地址”。

那么我如何以递归方式读取我的类别数组并在数据透视表中为每个类别输入上述三个字段?

1 个答案:

答案 0 :(得分:0)

我用以下代码解决了这个问题。

我只是拿着一个外部数组来记住每次迭代的位置,然后使用函数内部的另一个数组填充ids对应的类别信息并将其全部内容包含在内,这就是我需要解决的问题。作为slugs的数据透视表中的项是唯一的,并且可以用作SEO原因的路由参数。

private function recursive($categories, $depth=0)
{
  foreach($categories as $category) {
    array_push($this->locationHolder, $category['category_id']);
    if($depth == 0){
      $parent_id = 1;
    } else {
      $parent_id = $this->locationHolder[$depth-1];
    }
    $address_array = [];         
    foreach($this->locationHolder as $id) {
      array_push($address_array, Category::find($id)->slug);
    }
    DB::table('category_category')->insert([
      'category_id' => $category['category_id'],
      'parent_id' => $parent_id,
      'address' => implode("_", $address_array)
    ]);         
    if(isset($category['sub_categories'])) {
      $this->recursive($category['sub_categories'], $depth + 1);
    } else {
      array_pop($this->locationHolder);
    }
  }
  array_pop($this->locationHolder);
}