Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
A Detailed Info of Nested Set is Required
  • I need Detailed Example of NestedSet.
  • Harro VertonHarro Verton
    Accepted Answer
    Can you be more specific? What exactly do you want to know?

    Do you understand the principle behind nested sets? If not, read this first, so you get an understanding of how it works:
  • Exactly I got understand the basic principle.

    I already created a "Main menu", in my create menu option in application, when save button clicked, i want to create a menu "Second Menu"  under "Main Menu" as child. How it is possible?

    How i create a "Nested Set Instance" For doing so

  • Just to be clear, do you really mean "a second menu", for example one for a sidebar instead of a navbar, of do you mean "a submenu"?

    A second menu would mean a second tree structure with a new root, a submenu means inserting children of the submenu node in the existing tree.
  • I mean sub menu.

    Can i create Multiple menu's with root menu in one database using nestedset?

  • Harro VertonHarro Verton
    Accepted Answer
    Yes, you can create multiple tree structures in a single table, using the 'tree_id' column to uniquely identify the tree. You can use if field as a foreign key to a parent table that defines the trees (give them a name, etc), but you don't need to.

    You can add nodes to an existing tree without problems.

    If you look at the example given here:, you can see that it is very easy.

    Say you have a menu like this:
    - Item A
    - Item B
    - Item C

    and you want
    - Item A
    - Item B
      - Item B.1
      - Item B.2
    - Item C

    All you have to do is:

    // create the two new objects
    $b1 = Model_Menu::forge(array('name' => 'Item B.1'));
    $b2 = Model_Menu::forge(array('name' => 'Item B.2'));

    // get the node that has to become the parent (standard ORM query)
    $b = Model_Menu::query()->where('name', '=', 'Item B')->get_one();

    // and assign the new nodes to it (reverse order, child() inserts before)
  • How can i generate unique id for each menu name field. currently  i have to supply that. i would like to do it in this way

    If Sidebar Menu is Title, Then the name field must be sidebar_menu.
  • "name" was just an example, you can put anything in it, as long as the table has a left and right pointer, and optionally a tree_id.

    ORM models must always have a unique key. Just have it auto_increment, you don't need to use it. Using a title as key is not advised, it is not guaranteed to be unique, changing it means index updates, and string index creation and lookups are not very efficient.

    What do you mean with "how do you identify a menu"? If the table only has one tree, that is the menu. If it has multiple trees, the tree_id column (you can name it any way you want, for example menu_id) is the identifier of the tree. See on how you select the root of the tree.

    When retrieving a menu, you can use the dump_tree() method to fetch a complete tree in one go. You can then recurse over it, for example to create an unordered list HTML structure for your menu.
  • How i Fetch all Children and children of Children from nested set?

    I would like to get all in an array, means when i pass menu id, I would like to get all menu in an array,

    Dump_tree() only provides children, Not children of Children.
  • For people finding this thread, can you post what the issue was, and how you solved it?
  • exactly i used Debug::dump() to check the array returned using dump_tree(). This Debug::dump() not providing detailed children details, it showing there is children and ..... are showing, But when i flatten the array, i found there are all listed in array.
  • By default Debug::dump() only shows you 5 levels of nesting. You can change that by using

    Debug::$max_nesting_level = 10; // increase from 5 to 10

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion