0

我想根据带有子引用的模型树结构方法将以下数据结构(忽略 category_id、parent_id、位置和级别)输入 mongo Db:http: //docs.mongodb.org/manual/tutorial/model-tree -结构/

    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) 让您使用 categories[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();

对此方法的任何帮助将不胜感激。

谢谢

4

1 回答 1

0

编辑:有了所有新数据,我清楚地了解您要实现的目标。

所以我编辑了我的旧函数,这是一个应该在你的代码中正常工作的新函数。让我知道,以便我可以根据需要进行调整。

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;
    }

}
于 2013-01-11T10:56:24.587 回答