【问题标题】:Php Recursively entering categories to MongoDBPHP递归输入类别到MongoDB
【发布时间】:2013-01-12 02:34:29
【问题描述】:

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

    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();

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

谢谢

【问题讨论】:

    标签: php mongodb recursion yii


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢 DarkMukke,我添加了更多模型和我正在寻找的示例。我希望这是有道理的。我正在尝试遍历类别树,并将其保存为嵌入文档的数组。
    • $newCats 没有正确返回.. 孩子作为类别对象插入,它应该只是一个名称数组:例如:$data->categories[0]->children = array((string)"Root MySite.com", (string)"FooBar.com");
    • 你的数组中已经有了所有的数据,为什么还要再把它弄成一个类的对象呢?我以为你想将上述数据存储在 mongo 中
    猜你喜欢
    • 2012-03-18
    • 2016-07-18
    • 2021-11-24
    • 1970-01-01
    • 2022-11-27
    • 2014-03-20
    • 2014-10-22
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多