On Thu, Feb 5, 2009 at 4:43 PM, Dwayne <[email protected]> wrote:
>
> This idea seemed to work out. Basically what I needed to happen was to
> allow access to multiple controllers for data objects associated with
> a given page in the CMS. The data objects themselves are included in
> the page through a modified request action (the version by biesbjerg).
> So all I needed was to somehow set things up so that Cake knew which
> page we were on. Luckily I could access a custom class from within
> routes.php and use that to generate the base URLs and connect them to
> the proper controller/id combinations (this always uses the same
> action). As a bonus I can save these URLs in a singleton class so I
> don't need to query the DB again to figure out how to build links in
> the views (which did require a custom helper).
>
> The rough skeleton of what I did is in the following function:
>
>        function pageRoutes(){
>                $Page = new Page();
>
>                // get the tree list
>                $treelist = $Page->Behaviors->Tree->generatetreelist($Page, 
> NULL,
> '{n}.Page.id', '{n}.Page.alias', '/', 'Page.site_id,Page.lft ASC' );
>
>                // get all pages in the tree
>                $pages = $Page->find(
>                        'all',
>                        array(
>                                'conditions' => 'Page.id 
> IN('.join(',',array_keys($treelist)).')',
>                                'fields' => 
> 'Page.id,Page.site_id,Page.alias,Site.alias',
>                                'recursive' => 1
>                        )
>                );
>
>                // build array of data needed to make the paths
>                $page_data = array();
>
>                foreach($pages as $page){
>                        $page_data[ $page['Page']['id'] ] = array(
>                                'page_id' => $page['Page']['id'],
>                                'page_alias' => $page['Page']['alias'],
>                                'site_id' => $page['Page']['site_id'],
>                                'site_alias' => $page['Site']['alias'],
>                                'level' => 
> (substr_count($treelist[$page['Page']['id']],'/') )
>                        );
>                }
>
>                $path = array();
>                $prev_level = 0;
>                $prev_site = NULL;
>                $routes = array();
>                foreach($treelist as $pid => $level){
>                        // snip logic that builds the proper URLs for each page
>                        // and saves these for later use
>                }
>
>                // Sort routes in reverse key order so that the more specific 
> routes
> come first
>                krsort($routes);
>
>                foreach($routes as $url => $route_data){
>                        Router::connect($url,$route_data);
>                }
>        }
>
> I'm definitely not doing this in the most efficient manner possible,
> but it works and I can fix up the quirks later on. I'm pretty sure if
> I actually understood how the tree behaviour works I could just get
> the data I want in a single query ... but that can wait until later.
>
> Like you said, "needs refactoring".
>

Interesting. One thing that'll slow this down a bit is the SELECT IN.
If you're using MySQl, at least. I'm not sure about other DBs.

As for TreeBehavior, one thing that took me a while to figure out was
how to set up the initial left & right fields. But, once that was
done, everything "just worked".

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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