腓递归进入类别的MongoDB

问题描述:

我想进入下面的数据结构(忽略CATEGORY_ID,PARENT_ID,位置和等级)到蒙戈Db的根据自己的模型树结构的方法与孩子的引用:http://docs.mongodb.org/manual/tutorial/model-tree-structures/腓递归进入类别的MongoDB

object(Node)#1 (6) { 
    ["category_id"]=> 
    int(1) 
    ["parent_id"]=> 
    int(0) 
    ["name"]=> 
    string(4) "Root" 
    ["position"]=> 
    int(0) 
    ["level"]=> 
    int(0) 
    ["children"]=> 
    array(2) { 
    [0]=> 
    object(Node)#2 (6) { 
     ["category_id"]=> 
     int(2) 
     ["parent_id"]=> 
     int(1) 
     ["name"]=> 
     string(15) "Root MySite.com" 
     ["position"]=> 
     int(0) 
     ["level"]=> 
     int(1) 
     ["children"]=> 
     array(1) { 
     [0]=> 
     object(Node)#3 (6) { 
      ["category_id"]=> 
      int(15) 
      ["parent_id"]=> 
      int(2) 
      ["name"]=> 
      string(7) "Widgets" 
      ["position"]=> 
      int(68) 
      ["level"]=> 
      int(2) 
      ["children"]=> 
      array(2) { 
      [0]=> 
      object(Node)#4 (6) { 
       ["category_id"]=> 
       int(24) 
       ["parent_id"]=> 
       int(15) 
       ["name"]=> 
       string(11) "Blue Widget" 
       ["position"]=> 
       int(68) 
       ["level"]=> 
       int(3) 
       ["children"]=> 
       array(0) { 
       } 
      } 
      [1]=> 
      object(Node)#5 (6) { 
       ["category_id"]=> 
       int(25) 
       ["parent_id"]=> 
       int(15) 
       ["name"]=> 
       string(13) "Purple Widget" 
       ["position"]=> 
       int(68) 
       ["level"]=> 
       int(3) 
       ["children"]=> 
       array(0) { 
       } 
      } 
      } 
     } 
     } 
    } 
    [1]=> 
    object(Node)#6 (6) { 
     ["category_id"]=> 
     int(100) 
     ["parent_id"]=> 
     int(1) 
     ["name"]=> 
     string(10) "FooBar.com" 
     ["position"]=> 
     int(0) 
     ["level"]=> 
     int(1) 
     ["children"]=> 
     array(1) { 
     [0]=> 
     object(Node)#7 (6) { 
      ["category_id"]=> 
      int(150) 
      ["parent_id"]=> 
      int(100) 
      ["name"]=> 
      string(6) "Gizmos" 
      ["position"]=> 
      int(68) 
      ["level"]=> 
      int(2) 
      ["children"]=> 
      array(2) { 
      [0]=> 
      object(Node)#8 (6) { 
       ["category_id"]=> 
       int(240) 
       ["parent_id"]=> 
       int(150) 
       ["name"]=> 
       string(11) "Blue Gizmos" 
       ["position"]=> 
       int(68) 
       ["level"]=> 
       int(3) 
       ["children"]=> 
       array(0) { 
       } 
      } 
      [1]=> 
      object(Node)#9 (6) { 
       ["category_id"]=> 
       int(250) 
       ["parent_id"]=> 
       int(150) 
       ["name"]=> 
       string(13) "Purple Gizmos" 
       ["position"]=> 
       int(68) 
       ["level"]=> 
       int(3) 
       ["children"]=> 
       array(0) { 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

我不能拿出正确的php代码来遍历树并插入数据。

这是我到目前为止有:

public function behaviors() 
    { 
    return array(
     'embeddedArrays' => array(
      'class'=>'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior', 
      'arrayPropertyName' => 'categories', 
      'arrayDocClassName' => 'Category', 
     )); 
    } 

    public function setCategories($user_id) 
    { 
    $api = new ApiCategory($user_id); 
    $cats = $api->getCategories(); // retrieves structure above 

    $this->recursiveCategoryTree($cats); 

    $this->save(); // save the entire array of embedded documents 

    if($this->getErrors()) 
     var_dump($this->getErrors); 

    } 

    public function recursiveCategoryTree($tree, $i = 0) 
    { 
    foreach ($tree->children as $child) { 
     if ($count($child->children) > 0){ 
     $this->categories[$i] = new Category(); 
     $this->categories[$i]->_id = $tree->name; 
     $i++; 
     $this->getCategoryTree($child, $i); 
     } 
     else{ 
     $this->categories[--$i]->children[] = $child->name; 
     } 
    } 
    } 

我试过几种不同的方式来得到它的工作,但我不知道反应该如何工作的准确,或者也许我离开基地?我使用的php框架(http://canni.github.com/YiiMongoDbSuite/xhtml/basic.arrays.embedded-documents.html)使您可以使用类别[index]方法。根据该文件就应该像这样工作:

$data = new Data(); 
$data->categories[0] = new Category(); 
$data->categories[0]->_id = 'Root'; 
$data->categories[0]->children = array("Root MySite.com", "FooBar.com"); 
$data->categories[1]->_id = 'Root MySite.com'; 
$data->categories[1]->children = {immediate children}; 
$data->save(); 

用这种方法任何帮助,将不胜感激。

感谢

编辑: 与所有的新数据,我清楚地了解你想要达到的目的。

所以我编辑了我的老功能,这是一个新的,应该工作在两岸代码。 让我知道,所以我可以调整,如果需要。

public function setCategories($user_id) 
{ 
    $api = new ApiCategory($user_id); 
    $cats = $api->getCategories(); // retrieves structure above 

    $newCats = null; 
    self::recursiveCatTree($newCats, $cats); 
    $this->categories = $newCats; 

    $this->save(); // save the entire array of embedded documents 

    if($this->getErrors()) 
     var_dump($this->getErrors); 

} 

public static function recursiveCatTree(&$result, $parent) 
{ 
    $children = $parent['children']; 

    //we unset the children so we dont have manually set every other variable 
    unset($parent['children']); 
    $result = new Category(); 
    $result->attributes = $parent; 

    //then loop the children, if no children it wont loop 
    // so it will just be an empty array 
    foreach($children as $child) 
    { 
     $baby = null; 
     self::recursiveCatTree($baby, $child); 
     $result->children[] = $baby; 
    } 

} 
+0

Thanks DarkMukke,我增加了一些模型和我正在寻找的例子。我希望这是有道理的。我试图遍历类别树,并将其保存为嵌入文档的数组。 – bonez

+0

啊我明白你的意思,我会给你一个更新的一点 – DarkMukke

+0

我更新了我的方法,你的代码工作。 – DarkMukke