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 [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