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

  • Home
  • SEARCH
  • 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 5837977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:07+00:00 2026-05-22T11:25:07+00:00

I usually fetch NestedSet tree like this: class ModelTable extends Doctrine_Table { /** *

  • 0

I usually fetch NestedSet tree like this:

class ModelTable extends Doctrine_Table
{
  /**
   * Gets tree elements in one query
   */
  public function getMenuTree()
  {
    $q = $this->createQuery('p')
      ->orderBy('p.root_id')
      ->addOrderBy('p.lft');

    $tree = $q->execute(array(),  Doctrine_Core::HYDRATE_RECORD_HIERARCHY);      

    return $tree;
  }
}

So I can actually display the whole tree while using only one query to the database.. until I try to traverse the tree. For example, if you call a method on a node like this:

$node->getNode()->getAncestors()

Doctrine will build up a whole new query for this (have a look at Doctrine_Node_NestedSet::getAncestors()). Other traversing methods like getChildren() also use DQL. But this is somewhat inefficient, isn’t it? Once I fetched the whole tree, I don’t want to query the database any more.

Maybe someone has written a driver to do it the right way? (without DQL)

  • 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-05-22T11:25:08+00:00Added an answer on May 22, 2026 at 11:25 am

    If you only want to fetch the children (which is the most likely, why would you need getAncestors() to iterate on a tree?), you could also keep the code you showed us as example, and do something like this this:

    foreach ($categories->getFirst()->get('__children') as $child) {
        // ...
    }
    

    This is documented here (hard to find unless you choose to read the whole documentation).

    I have once used recursive code on a whole tree with only ONE query.

    1015 lib % ack --type="php" "_node"                                                                                                                                                          2011-05-15 14:26:22 greg pts/1
    vendor/doctrine/Doctrine/Record.php
    94:    protected $_node;
    814:        unset($vars['_node']);
    2403:        if ( ! isset($this->_node)) {
    2404:            $this->_node = Doctrine_Node::factory($this,
    2410:        return $this->_node;
    liche ~/source/symfony/1.4/lib/plugins/sfDoctrinePlugin/lib
    

    _node only seems to be set in getNode() itself, I don’t know whether you can hydrate it like any other field, nor how you would do this.

    I think that getNode() should only be used for modifications on the tree.
    If you want to display the path from the root, you should use a recursive method to display the tree, whith an argument containing the parent’s path . If there is anything else for which you would need the tree functionality, tell us…

    UPDATE

    I think I eventually got it. You want to display a tree menu AND a breadcrumb, and you want to reuse the data of the menu in the breadcrumb, isn’t it?
    To display your breadcrumb, you have to recurse on $tree, and display a node if and only if it is an ancestor of the current page. And there is a method for that : isAncestorOf().
    So “all you have to do” is a template which does something like this:

    //module/templates/_breadcrumbElement.php
    foreach ($node->get('__children') as $child) :
      if ($child->isAncestorOf($pageNode)):
         echo link_to($child->getName(), $child->getUrl());
         include_partial('module/breadcrumbElement', array('node' => $child, 'pageNode' => $pageNode));
      endif;
    endforeach;
    

    Feed it the root of your tree and you’ll be fine. Hopefully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually when I need to fork in C, I do something like this: pid_t
Usually when my regex patterns look like this: http://www.microsoft.com/ Then i have to escape
this query will just one row as a result myDataContext db = new myDataContext();
I usually do not have difficulty to read JavaScript code but for this one
Usually when I build a site, I put all the CSS into one file,
I'm running the Google App Engine devserver 1.3.3 on Windows 7. Usually, this method
Joins are usually used to fetch data from 2 tables using a common factor
Usually, when an application writes to one of it's files on disk, the file
Usually when one wants to create a new file in the Eclipse IDE ,
I'm stuck on an mysql query. I usually just put the result set into

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.