I am fairly new to CakePHP and have decent experience with MVC, but
the issue of navigation always seems to haunt me. I wanted to run my
navigation idea by anybody who cares to comment, to see if I am
completely over thinking the whole thing.

My example has been simplified for clarity.

What I have is a table in the database page_descriptions that has a
parent child relationship.

CREATE TABLE  `page_descriptions` (
  `id` int(11) NOT NULL auto_increment,
  `parent_id` int(11) NOT NULL,
  `sort_order` int(11) default NULL,
  `long_id` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `status_id` int(11) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

My model for this looks like this:

page_description.php
<?php
class PageDescription extends AppModel {

        var $name = 'PageDescription';
        var $validate = array(
                        'page_id' => VALID_NOT_EMPTY,
                        'name' => VALID_NOT_EMPTY,
                        'long_id' => VALID_NOT_EMPTY,
                                 );
         var $displayField = 'name';
         var $belongsTo  = array(
                        'ParentPage' =>
                                array('className' => 'PageDescription',
                                          'foreignKey' => 'parent_id',
                                          'fields'        => '`id`, 
`parent_id`, `long_id`, `name`',
                        )
                );

         var $hasMany = array(
                        'ChildPages' =>
                                        array('className'     => 
'PageDescription',
                                                  'foreignKey'    => 
'parent_id',
                                                  'fields'        => '`id`, 
`parent_id`, `long_id`, `name`',
                                                  'order'         => 
'`sort_order` DESC',
                                                  'exclusive'     => 'false',
                                                  'dependent'     => 'true',
                                )
                        );

        function findByLongId($long_id = null){
                if ($long_id) {
                        $ret = $this->query("SELECT * FROM page_descriptions 
WHERE long_id
= '".$long_id."'");
                        if ($ret) {
                        return $ret[0];
                        } else {
                                return null;
                        }
                }
        }
        /*
                Navigation Functions
        */

        function getNavLevel($int=1){
                //return $this->PageDescription->findAllByParentId(1) ;
                return $this->PageDescription->findAll("
`PageDescription`.`parent_id` = ".$int." AND
`PageDescription`.`status_id` > 0 ",
null,"`PageDescription`.`sort_order`", null,null,2) ;
        }
}
?>

I have edited the routes.php file and added this before last line.

        $Route->connect('/*', array('controller' => 'pages', 'action' =>
'display'));

So everything not explicitly declared with a controller and action
before hand is sent to the pages_controller.

What I do is use the params['pass'] to get the long_id and use that to
find the specific PageDescription record. Currently this requires
every long_id to be unique, but I have removed the code that resolves
this issue for simplification.

I am also using a half assed "singleton" pattern to query the database
once per session and store this information in a session variable.

OK, so here are my questions.

1. Is there an easier way that will allow for a flexible database
driven navigation system?

2. When I return the PageDescription record, I can set recursion to
get as many levels up of the ParentPage as I could hope for, but the
ChildPages only goes one level deep no matter how I have adjusted the
model.

3. Does anybody have any good examples of how they have handled
something similar.

I appreciate your help.

Thanks,

James


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

Reply via email to