Saturday, March 30, 2013

RE: Product / Category Tree Help

What I ended up doing is adding "path" field to the table and a pretty simple function to build the path for each category.

 

It needs to be cleaned up some for contain and cache but initial foundation appears to be in place. So this will build out the actual path no matter how deep the tree is.

buildPath() was just to build the initial paths for the new field in the db. When a category is created / modified the path will be automatically created on save.

 

Controller:

$this->Category->build_path();

 

Model:

public function build_path(){

                $categories = $this->find('all');

                               

                foreach ($categories as $k => $v){

                                $path = '';

                                $nest = $this->getPath($v['Category']['id']);

 

                                foreach($nest as $kk => $vv){

                                                $path .= $vv['Category']['slug'] . '/';

                                }

                $this->id = $v['Category']['id'];

                $this->saveField('path', $path);

                }

                               

}

 

And for the breadcrumbs:

<?php foreach ($path as $k['Category'] => $v):?>

               

<?php $this->Html->addCrumb($v['Category']['name'], array('controller' => 'categories', 'action' => 'view', $v['Category']['path']));?>

 

<?php endforeach; ?>

 

So in the end I get my respective paths such as:

'all/'

'all/men/'

'all/men/shoes/'

'all/men/sweaters/'

'all/men/watches/'

'all/men/jackets/'

'all/men/jeans/'

'all/men/accessories/'

'all/women/'

'all/women/shoes/'

'all/women/accessories/'

'all/women/jackets/'

'all/women/sweaters/'

'all/women/watches/'

'all/women/jeans/'

'all/men/shirts/'

'all/children/'

'all/children/shoes/'

'all/children/jackets/'

'all/children/jeans/'

'all/children/shirts/'

'all/women/shirts/'

'all/men/jackets/leather/'

'all/men/jackets/leather/black/'

'all/men/jackets/leather/brown/'

 

Still open to other ideas if there is something better or suggestions.

 

Thanks,

 

Dave

 

 

From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of Salines
Sent: Saturday, March 30, 2013 11:13 AM
To: cake-php@googlegroups.com
Subject: Re: Product / Category Tree Help

 

Create the following db tables:

 

table nodes, where you will have a minimum of the following fields: 

id, title, path, parent_id, lft, rght, type

 

In the field: path, save the full path on which you will find your posts.

Field: type, is reserved for storing content types, eg '1 '. categories, '2 '. products,..

 

 

table closures, view presentation

 

table posts, where you store the product description, etc

 

table atachments, where you store the product pictures..

 

 

Routing like this

 

Router::connect('/:path/*', array('admin'=>false,'controller' => 'nodes', 'action' => 'show'),array('pass'=>array('path','page'),'page' =>'[0-9]+'));

 

 

Node::show();

 

            public function show() {

                        $path = func_get_args();

                        //debug($path);

                        $last_arg = end(array_values($path));

 

                        if (is_numeric($last_arg)) {

                                    $page = $last_arg;

                                    //debug($page);

                                    array_pop($path);

                                    $path = implode("/", $path);

                                    $this -> request -> params['named']['page'] = $last_arg;

                        } else {

                                    $path = implode("/", $path);

                        }

....

}

Regards, Nikola

 

 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: