generateNestedList
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(
$this->name.'.id',$this->name.'.'.$showField,$this->name.'.parent_id'),$sort);
$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;
}
on March 15, 2007 on 11:07 am
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.
Cheers.
on March 15, 2007 on 11:08 am
The above view code should have been…
echo $html->selectTag(‘Node/parent_id’, $category_tree, $html->tagValue(‘Node/parent_id’), array(), array(), true);
on March 15, 2007 on 1:24 pm
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.
on March 17, 2007 on 4:55 am
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,
Brett.
on September 27, 2007 on 6:10 pm
Does this code is still valid? I can’t get it working.