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: 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) {
}
}
}
}
}
}
}
}
I can’t quite come up with the right php code to traverse the tree and insert the data.
Here is what I have so far:
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;
}
}
}
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:
$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();
Any help with this method would be appreciated.
Thanks
EDIT:
With all the new data, i clearly understand what you are trying to achieve.
So i edited out my old function, this is a new one that should work strait in your code.
Let me know so i can adjust if needed.