What about something like this:

class SectionController extends AppController
{
        var $uses = array('Section');

        function view($nodeId)
        {
                $tree = $this->Section->findAllThreaded();

                $breadCrumb = $this->_findBreadCrumb($tree, $nodeId);

                echo '<pre>'; print_r($breadCrumb); echo '</pre>';
                exit;
        }

        function _findBreadCrumb(&$tree, $nodeId, $currentPath = array())
        {
                $breadCrumb = array();

                foreach($tree as $node)
                {
                        $currentNode = $node['Section'];

                        if ($currentNode['id'] == $nodeId)
                        {
                                $breadCrumb = $currentPath;

                                $breadCrumb[] = $currentNode;

                                return $breadCrumb;
                        }
                        else if (isset($node['children']) &&
count($node['children']) > 0)
                        {
                                $currentPath[] = $currentNode;

                                $result =
$this->_findBreadCrumb($node['children'], $nodeId, $currentPath);

                                if (count($result) > 0)
                                {
                                        return $result;
                                }
                        }
                }

                return $breadCrumb;
        }
}

If for example on the DB you have something like:

Articles (ID 1) > Programming (ID 3) > PHP (ID 7) > CakePHP (ID 8)

And you are looking for ID 7 (PHP) $breadCrumb should be:

array(
        array(
                'id' => 1,
                'name' => 'Articles'
        ),
        array(
                'id' => 3,
                'name' => 'Programming'
        ),
        array(
                'id' => 1,
                'name' => 'PHP'
        )
)

I haven't tested this, so your computer may burst into flames ;) Just
kidding, it should work.

-MI

---------------------------------------------------------------------------

Remember, smart coders answer ten questions for every question they ask. So be smart, be cool, and share your knowledge.
BAKE ON!


-----Mensaje original-----
De: [email protected] [mailto:[EMAIL PROTECTED] En nombre
de phirschybar
Enviado el: MiƩrcoles, 27 de Diciembre de 2006 01:20 a.m.
Para: Cake PHP
Asunto: Re: Reverse recursive findAllThreaded queries?

So, essentially you're trying to find "breadcrumbs" to your selection?

Maybe a function could be added to the tree helper. Like..


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to