Note that there are some explanatory texts on larger screens.

plurals
  1. POPhp Recursively entering categories to MongoDB
    text
    copied!<p>I want to enter the following data structure (ignoring category_id, parent_id, position, and level) into mongo Db according to their model tree structures method with child references: <a href="http://docs.mongodb.org/manual/tutorial/model-tree-structures/" rel="nofollow">http://docs.mongodb.org/manual/tutorial/model-tree-structures/</a></p> <pre><code> object(Node)#1 (6) { ["category_id"]=&gt; int(1) ["parent_id"]=&gt; int(0) ["name"]=&gt; string(4) "Root" ["position"]=&gt; int(0) ["level"]=&gt; int(0) ["children"]=&gt; array(2) { [0]=&gt; object(Node)#2 (6) { ["category_id"]=&gt; int(2) ["parent_id"]=&gt; int(1) ["name"]=&gt; string(15) "Root MySite.com" ["position"]=&gt; int(0) ["level"]=&gt; int(1) ["children"]=&gt; array(1) { [0]=&gt; object(Node)#3 (6) { ["category_id"]=&gt; int(15) ["parent_id"]=&gt; int(2) ["name"]=&gt; string(7) "Widgets" ["position"]=&gt; int(68) ["level"]=&gt; int(2) ["children"]=&gt; array(2) { [0]=&gt; object(Node)#4 (6) { ["category_id"]=&gt; int(24) ["parent_id"]=&gt; int(15) ["name"]=&gt; string(11) "Blue Widget" ["position"]=&gt; int(68) ["level"]=&gt; int(3) ["children"]=&gt; array(0) { } } [1]=&gt; object(Node)#5 (6) { ["category_id"]=&gt; int(25) ["parent_id"]=&gt; int(15) ["name"]=&gt; string(13) "Purple Widget" ["position"]=&gt; int(68) ["level"]=&gt; int(3) ["children"]=&gt; array(0) { } } } } } } [1]=&gt; object(Node)#6 (6) { ["category_id"]=&gt; int(100) ["parent_id"]=&gt; int(1) ["name"]=&gt; string(10) "FooBar.com" ["position"]=&gt; int(0) ["level"]=&gt; int(1) ["children"]=&gt; array(1) { [0]=&gt; object(Node)#7 (6) { ["category_id"]=&gt; int(150) ["parent_id"]=&gt; int(100) ["name"]=&gt; string(6) "Gizmos" ["position"]=&gt; int(68) ["level"]=&gt; int(2) ["children"]=&gt; array(2) { [0]=&gt; object(Node)#8 (6) { ["category_id"]=&gt; int(240) ["parent_id"]=&gt; int(150) ["name"]=&gt; string(11) "Blue Gizmos" ["position"]=&gt; int(68) ["level"]=&gt; int(3) ["children"]=&gt; array(0) { } } [1]=&gt; object(Node)#9 (6) { ["category_id"]=&gt; int(250) ["parent_id"]=&gt; int(150) ["name"]=&gt; string(13) "Purple Gizmos" ["position"]=&gt; int(68) ["level"]=&gt; int(3) ["children"]=&gt; array(0) { } } } } } } } } </code></pre> <p>I can't quite come up with the right php code to traverse the tree and insert the data.</p> <p>Here is what I have so far:</p> <pre><code> public function behaviors() { return array( 'embeddedArrays' =&gt; array( 'class'=&gt;'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior', 'arrayPropertyName' =&gt; 'categories', 'arrayDocClassName' =&gt; 'Category', )); } public function setCategories($user_id) { $api = new ApiCategory($user_id); $cats = $api-&gt;getCategories(); // retrieves structure above $this-&gt;recursiveCategoryTree($cats); $this-&gt;save(); // save the entire array of embedded documents if($this-&gt;getErrors()) var_dump($this-&gt;getErrors); } public function recursiveCategoryTree($tree, $i = 0) { foreach ($tree-&gt;children as $child) { if ($count($child-&gt;children ) &gt; 0 ){ $this-&gt;categories[$i] = new Category(); $this-&gt;categories[$i]-&gt;_id = $tree-&gt;name; $i++; $this-&gt;getCategoryTree($child, $i); } else{ $this-&gt;categories[--$i]-&gt;children[] = $child-&gt;name; } } } </code></pre> <p>I've tried several different ways to get it to work, but I'm not sure how the counter should work exactly, or maybe I'm way off base? The php framework I'm using (http://canni.github.com/YiiMongoDbSuite/xhtml/basic.arrays.embedded-documents.html) makes you use the categories[index] method. According to the documentation it should work like this:</p> <pre><code>$data = new Data(); $data-&gt;categories[0] = new Category(); $data-&gt;categories[0]-&gt;_id = 'Root'; $data-&gt;categories[0]-&gt;children = array("Root MySite.com", "FooBar.com"); $data-&gt;categories[1]-&gt;_id = 'Root MySite.com'; $data-&gt;categories[1]-&gt;children = {immediate children}; $data-&gt;save(); </code></pre> <p>Any help with this method would be appreciated. </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload