Hey Max,

well behaviours are great in terms of code reusability.
They stand to modules as components stand to controllers.

Just a small example. If you checked out Cake 1.2 you might have
noticed the Tree behaviour.
The Tree behaviour helps to handle situations that deel with commplex
hierarchical data
(have a look at http://www.sitepoint.com/article/hierarchical-data-database/2)

In the specific case of the Tree behaviour, some methods might be
common to several models. In order to do not duplicate code, it is
therefore better to have a class with some methods that can be re-used
in several situations.

To use behaviours, in a model just add the following variable on top.
-----------------------------------------------------
var $actsAs = array('Tree'); // <-- for the Tree Behaviour
-----------------------------------------------------
In this specific example, every time you add a new record, the Tree
Behaviour will fill in the rght, and lft field of your database table
depending on the parent tree in the hierarchy (+ it will do more stuff
in case you delete or mode the item from a certain node of the tree)

Try out this model (you bake a Trees_controller and relative views)
------------------------------------------------------------------------------------------------
Tree.php
------------------------------------------------------------------------------------------------
<?php
class Tree extends AppModel {

        var $name = 'Tree';
        var $actsAs = array('Tree');

        var $belongsTo = array(
                        'Parent' =>
                                array('className' => 'Tree',
                                                'foreignKey' => 'parent_id',
                                                'conditions' => '',
                                                'fields' => '',
                                                'order' => '',
                                                'counterCache' => ''
                                ),

        );

}
?>
------------------------------------------------------------------------------------------------
DB TABLE
------------------------------------------------------------------------------------------------
CREATE TABLE `trees` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(200) NOT NULL,
  `parent_id` int(11) default NULL,
  `lft` int(11) default NULL,
  `rght` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


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