Re: Reverse recursive findAllThreaded queries?

2007-02-01 Thread admc

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: cake-php@googlegroups.com [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 cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2007-01-01 Thread gremlin


gwoo that is the model I used. Nothing I have found works to lookup the
id of a category  when the category tree is passed via the url as
params. The function crazylegs wrote is logically sound but why would I
execute N queries to get to the id only to use it to do lookups against
other tables?

Just to clarify the logic is all in the model. The controller is only
calling the model function and passing the params in.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-28 Thread the_woodsman


I'm with Gremlin on this one -
When working with trees of unknown depth, it's essential to get them
out in one query, and looping to create SQL is the only way I know.

But whenever I think Cake can't do something, a fellow Baker on this
board usually corrects me!
Is there a way to leverage some of Cake's nice abstraction to get a
tree of unknown depth, without querying one at a time?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-28 Thread gwoo


Probably the best way is through self joins in the model.

For Example, the Category model from the Bakery.
https://cakeforge.org/plugins/scmsvn/viewcvs.php/trunk/bakery/models/category.php?rev=352root=bakeryview=markup


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



RE: Reverse recursive findAllThreaded queries?

2006-12-27 Thread Mariano Iglesias


So your computer didn't burst into flames?

Pheee Good to know :)

-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: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
de [EMAIL PROTECTED]
Enviado el: Miércoles, 27 de Diciembre de 2006 03:23 a.m.
Para: Cake PHP
Asunto: Re: Reverse recursive findAllThreaded queries?

Ok, I tried this and everything looks very nice (after the small amount
of testing I did). The result is definately workable.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-27 Thread CraZyLeGs


a Model is more suitable for this kind of stuff.

here is my contribution:

function findPath($id)
{
static $path = array();

if($id == 0)
{
if(!empty($path) || count($path)  1)
{
$path = array_reverse($path);
}
return $path;
}

$row =
$this-find(array('Node.id'=$id),array('Node.id','Node.parent_id','Node.name'));

if(!$row)
{
return array();
}

$path[] = $row;

$parent_id = $row['Node']['parent_id'];

return $this-findPath($parent_id);

}


@gremlin: eww raw queries, how ugly ;)

On Dec 27, 9:12 pm, gremlin [EMAIL PROTECTED] wrote:

Not sure if it helps you at all but I once needed to know the ID of the
current category in a tree structure where the 'token' of the category
was the url param and not the id.

IE.http://server.com/categories/art/painting/van_gogh
and I needed to resolve the id of the category 'van_gogh' in order to
look up related records in other tables.

I used this in my category model.

function setCategory( $c = null )
{
if( is_array( $c ) )
{
$this-id = 1;
if( count( $c )  0 )
{
$d = array_values( $c );
$t = array_pop( $d );

$query = SELECT parent_0.id FROM categories as parent_0 
;
$where = WHERE parent_0.token = ' . $t . ' ;
$loop = 1;
while( count( $d )  0 )
{
$t = array_pop( $d );
$query = $query . LEFT JOIN categories as parent_ 
. $loop .  ON
parent_ . ($loop-1) . .parent_id = parent_ . $loop . .id ;
$where = $where . AND parent_ . $loop . .token = ' . 
$t . ' ;
$loop++;
}

$category_id = $this-query( $query . $where );
if( $category_id )
{
$this-id = $category_id[0]['parent_0']['id'];
}
else
{
$this-id = -1;
}
}
}

}Where the category had id, token, created, modified, description, and
parent_id fields.

In my controller I look up the category params like so.

function index( $path = null )
{
 $this-Category-setCategory( $function_args );
 $this-set( 'category_data',$this-Category-findAll(
Category.id = $this-{Category-id}, null, null, 0 )  );

}



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Reverse recursive findAllThreaded queries?

2006-12-26 Thread [EMAIL PROTECTED]


I have a situation where I know the id of a child, but I need the
parental hierarchy. Using the structure in this (excellent) Bakery
example, I have the following:

1. Art
 1. Film
 2. Music
   1. Jazz
   2. Pop
2. History
 1. Archaeology
 2. War
3. Science
 1. Biology
 2. Chemistry
 3. Physics
4. Technology
 1. Computing
   1. Hardware
   2. Software
 2. Engineering

In my situation, I know of Hardware, but I need to know the
following:

4. Technology
 1. Computing
   1. Hardware

Or if I know of Physics, I need to know:

3. Science
 3. Physics

Does anyone know how to accomplish this in a Cake-ish way (without
hacking a bunch of queries together in my controller)?

Thanks in advance!


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-26 Thread phirschybar


Are all of the items in your hierarchy one type of data? In other
words, are they all in one table with an association of has and
belongs to many?

Ben

On Dec 26, 7:32 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

I have a situation where I know the id of a child, but I need the
parental hierarchy. Using the structure in this (excellent) Bakery
example, I have the following:

1. Art
  1. Film
  2. Music
1. Jazz
2. Pop
2. History
  1. Archaeology
  2. War
3. Science
  1. Biology
  2. Chemistry
  3. Physics
4. Technology
  1. Computing
1. Hardware
2. Software
  2. Engineering

In my situation, I know of Hardware, but I need to know the
following:

4. Technology
  1. Computing
1. Hardware

Or if I know of Physics, I need to know:

3. Science
  3. Physics

Does anyone know how to accomplish this in a Cake-ish way (without
hacking a bunch of queries together in my controller)?

Thanks in advance!



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-26 Thread [EMAIL PROTECTED]


Yes, there is a single table that references itself with a parent_id
field. This is pratically identical to how it laid out in this example:
http://bakery.cakephp.org/articles/view/63

On Dec 26, 5:08 pm, phirschybar [EMAIL PROTECTED] wrote:

Are all of the items in your hierarchy one type of data? In other
words, are they all in one table with an association of has and
belongs to many?

Ben

On Dec 26, 7:32 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 I have a situation where I know the id of a child, but I need the
 parental hierarchy. Using the structure in this (excellent) Bakery
 example, I have the following:

 1. Art
   1. Film
   2. Music
 1. Jazz
 2. Pop
 2. History
   1. Archaeology
   2. War
 3. Science
   1. Biology
   2. Chemistry
   3. Physics
 4. Technology
   1. Computing
 1. Hardware
 2. Software
   2. Engineering

 In my situation, I know of Hardware, but I need to know the
 following:

 4. Technology
   1. Computing
 1. Hardware

 Or if I know of Physics, I need to know:

 3. Science
   3. Physics

 Does anyone know how to accomplish this in a Cake-ish way (without
 hacking a bunch of queries together in my controller)?

 Thanks in advance!



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-26 Thread phirschybar


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

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

/// Usage in view:

$tree-showCrumbs(99, 'Section/name', $data);

/// New function in tree helper class:

function showCrumbs($id, $name, $data)
 {
   /// .. something clever with a recursive use of php array_keys()
function
   /// .. would have to track back to the parent returning the names
and ids
   /// .. of the sections. Thoughts anyone?
 }

On Dec 26, 10:35 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

Yes, there is a single table that references itself with a parent_id
field. This is pratically identical to how it laid out in this 
example:http://bakery.cakephp.org/articles/view/63

On Dec 26, 5:08 pm, phirschybar [EMAIL PROTECTED] wrote:

 Are all of the items in your hierarchy one type of data? In other
 words, are they all in one table with an association of has and
 belongs to many?

 Ben

 On Dec 26, 7:32 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

  I have a situation where I know the id of a child, but I need the
  parental hierarchy. Using the structure in this (excellent) Bakery
  example, I have the following:

  1. Art
1. Film
2. Music
  1. Jazz
  2. Pop
  2. History
1. Archaeology
2. War
  3. Science
1. Biology
2. Chemistry
3. Physics
  4. Technology
1. Computing
  1. Hardware
  2. Software
2. Engineering

  In my situation, I know of Hardware, but I need to know the
  following:

  4. Technology
1. Computing
  1. Hardware

  Or if I know of Physics, I need to know:

  3. Science
3. Physics

  Does anyone know how to accomplish this in a Cake-ish way (without
  hacking a bunch of queries together in my controller)?

  Thanks in advance!



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



RE: Reverse recursive findAllThreaded queries?

2006-12-26 Thread Mariano Iglesias


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: cake-php@googlegroups.com [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 cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-26 Thread [EMAIL PROTECTED]


I like that idea, I'll try it out (and fix anything that is broken).

Thanks Mariano!


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Reverse recursive findAllThreaded queries?

2006-12-26 Thread [EMAIL PROTECTED]


Ok, I tried this and everything looks very nice (after the small amount
of testing I did). The result is definately workable.

Thanks again Mariano for your help.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---