I recommend you use Modified Preorder Tree Traversal (MPTT)

http://www.ad7six.com/entries/view/56/Working-with-Tree-data-%28MPTT%29
http://articles.sitepoint.com/print/hierarchical-data-database
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

Here's an example of an app that has many Sections stored as a tree,
each with 0 or more Items.


Section model:

public $actsAs = array(
        'Tree' => array(
                'parent' => 'parent_id',
                'left' => 'lft',
                'right' => 'rght'
        ),
        'Sluggable' => array(
                'translation' => 'utf-8',
                'separator' => '_',
                'label' => array('name'),
                'length' => 128,
                'overwrite' => true
        )
);

public function threaded($section_ids = null)
{
        $filters = array(
                'conditions' => array('Section.parent_id IS NOT NULL'),
                'fields' => array('*'),
                'order' => array('Section.lft' => 'ASC'),
                'contain' => array(
                        'Item' => array(
                                'fields' => array('Item.id'),
                                'order' => array('Item.name' => 'ASC')
                        )
                )
        );

        if (!is_null($section_ids))
        {
                $filters['conditions'] = array(
                        'Section.id' => $section_ids
                );
        }

        return $this->find('threaded', $filters);
}

SectionsController:

public $helpers = array('Tree', 'Cache');

SQL:

DROP TABLE IF EXISTS sections;
CREATE TABLE sections
(
    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL,
    parent_id INT(10) DEFAULT NULL,
    lft INT(10) NOT NULL,
    rght INT(10) NOT NULL,
    name VARCHAR(128) NOT NULL,
    slug VARCHAR(255) NOT NULL,
    description TEXT DEFAULT NULL,
    section_illustration_id INT(10) DEFAULT NULL,

    KEY (section_illustration_id)
) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

DROP TABLE IF EXISTS section_illustrations;
CREATE TABLE section_illustrations
(
    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL,
    basename VARCHAR(64) NOT NULL,
    directory VARCHAR(255) NOT NULL,
    width VARCHAR(3) DEFAULT NULL,
    height VARCHAR(3) DEFAULT NULL,
    type VARCHAR(64) NOT NULL,
    size INT(11) UNSIGNED NOT NULL
) ENGINE=MyISAM;

DROP TABLE IF EXISTS items;
CREATE TABLE items
(
    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL,
    section_id INT(10) NOT NULL,
    name VARCHAR(128) NOT NULL,
    slug VARCHAR(128) NOT NULL,
    description TEXT DEFAULT NULL,

    KEY (section_id)
) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;



On Feb 20, 5:45 pm, "Dave" <make.cake.b...@gmail.com> wrote:
> I am starting in on a products controller for the first time and looking for
> some advice on best approach.
>
> Thought about using tree behaviour. My products will be in "levels" 3 deep
> Parent categorgy
>     /child
>         /grandchild i guess?
>
> Winter
>     /Boots
>         /Mens
>
> type idea.
>
> Whats the best way to do this that any of you have found along the way? Any
> tips / sites / tutorials
>
> Thanks,
>
> Dave

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to