Well, my computer didn't burn in flames, but my brain did...
I needed the same solution for my code and found this by Google. I
made a component of it, because I needed the breadcrumb for two
models, and now it can be used by anyone, working, tested:
class BreadComponent extends Object
{
var $controller = true;
function _findBreadCrumb(&$tree, $nodeId, $modelname, $currentPath =
array())
{
$breadCrumb = array();
foreach($tree as $node)
{
$currentNode = $node[$modelname];
if ($currentNode['id'] == $nodeId)
{
$breadCrumb = $currentPath;
$breadCrumb[] = $currentNode;
return $breadCrumb;
}
else if (isset($node['children']) &&
count($node['children']) > 0)
{
$newCurrentPath=$currentPath;
$newCurrentPath[] = $currentNode;
$result=$this->_findBreadCrumb($node['children'], $nodeId,
$modelname, $newCurrentPath);
if (count($result)>0)
return $result;
}
}
return $breadCrumb;
}
}
I added a new parameter "$modelname", that's the Cake name of the
model to be searched. And the reason why my brain burned into flames
was $currentPath, that made a confusion and the function gave a bad
result. (So I don't know how it worked for "xeeton"...) The new nodes
was appended to $currentPath even if they were not relevant. Now it's
using another variable so it works good.
By the way, I found Mariano's solution the best of the listed ones
(and of the Google Search Result, too :). Gremlin's one uses few
CakePHP, it may be good on a PHP-only mailing list. :)
Cheers,
Adam
Mariano Iglesias írta:
> 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
-~----------~----~----~----~------~----~------~--~---