Oth\’s Thoughts


Posted in CakePHP by othy on June 3, 2006

UPDATE: 15/03/2007 snippet updated. Before it was only working with tables that has a field named ‘name’ now it supports name, title and the value of Model::displayField
also introduced conditions which makes it a bit incompatible with the old unless you call it without params since a new param has been introduced. If anyone has a tip on how to indent code properly in WP please let me know.

This method does the same thing as generateList, except that it’s used to return a hierachy

its main use is with select tags

NOTE: generateNestedList assumes a parent_id field, and a record with parent_id = 0 is a root field. put the two methods in your AppModel

function generateNestedList($conditions = null,$sort = null,$indent = '--')
$showField = ($this->hasField('name'))?'name':(($this->hasField('title'))?'title':$this->displayField);
$this->recursive = '-1';
$cats = $this->findAllThreaded($conditions,array(
$glist = $this->_generateNestedList($cats,$indent,$showField); return $glist;
function _generateNestedList($cats,$indent,$showField,$level = 0)
static $list = array();
for($i = 0, $c = count($cats); $i < $c; $i++)
$list[$cats[$i][$this->name]['id']] = str_repeat($indent,$level).$cats[$i][$this->name][$showField];
if(isset($cats[$i]['children']) && !empty($cats[$i]['children']))
$this->_generateNestedList($cats[$i]['children'],$indent,$showField,$level + 1);
return $list;

5 Responses to 'generateNestedList'

Subscribe to comments with RSS or TrackBack to 'generateNestedList'.

  1. Brett said,

    Hi there,

    I’ve been trying to get this bit of code working in my cake app for the past few days with no luck. I’m using version 1.1.13.

    I’ve got the above snippet of code in my app_model.php file.

    In my controller I’ve got:
    $this->set(‘category_tree’, $this->Node->generateNestedList());

    And in my view I’ve got:
    selectTag(‘Node/parent_id’, $category_tree, $html->tagValue(‘Node/parent_id’), array(), array(), true);?>

    Problem is that the $category_tree is always empty, I’m obviously missing something here. If I use the same setup with generateList everything goes to plan.

    Any help would be appreciated.


  2. Brett said,

    The above view code should have been…

    echo $html->selectTag(‘Node/parent_id’, $category_tree, $html->tagValue(‘Node/parent_id’), array(), array(), true);

  3. othy said,

    Hi Brett,

    It was working fine here, but anyway, I updated the snippet copy/paste from above now, see if it works. If not, please call findAllThreaded directly and check if it’s returning results, also make sure you actually have an hierarchy etc. you can also debug the code and see if it has a problem somewhere.

  4. Brett said,

    Hi again,

    I’ve tried the updated code and still no joy, it must be something to do with my app setup here.

    I called findAllThreaded directly and it does return an array correctly. The only other thing I’ve noticed is that there’s an sql error down the bottom when I put it in debug mode:
    QUERY: generateNestedList
    ERROR: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘generateNestedList’ at line 1

    For some reason it’s trying to query the database with ‘generateNestedList’ ???

    My nodes table looks like this:

    Bare with me, I’m a designer not a coder so a lot of this is new to me đŸ˜‰

    Thanks again,


  5. Izrul said,

    Does this code is still valid? I can’t get it working.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: