Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:42:59+00:00 2026-06-17T06:42:59+00:00

I want to enter the following data structure (ignoring category_id, parent_id, position, and level)

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:43:00+00:00Added an answer on June 17, 2026 at 6:43 am

    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.

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display information after I enter data into the database using ajax
Scenario: I want to enter an address (Sweden, Stockholm) into an inputfield and get
I am trying to create a list where I want users to enter data
I am using the following statement to load data from a file into a
I have the following issue: I enter data in a UITextField and then I
I want to enter floating point values to some of the record. So if
In my application four TextArea is there and I want to enter only four
I have a text-box, and I want to enter a string in language A
This is a mystery.. in my controller Add function i want to enter the
I want to interpret Enter key as Tab key in whole my WPF application,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.