Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Forming Tree structure in cakephp

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 204
    Comment on it

    Sometime you may need to form a tree structure from the values of the database table. For Ex- You may need to form a hierarchy of categories and subcategories like this:

    My Categories
    
        Fun
            Sport
                Surfing
                Extreme knitting
            Friends
                Gerald
                Gwendolyn
        Work
            Reports
                Annual
                Status
            Trips
                National
                International
    

    This can be easily achieved by cakephp's tree behavior

    Requirement: For this purpose you need 3 column in the database table

    1) parent : int  type
    2) lft int type
    3) rght - int</p>
    
    CREATE TABLE categories (
        id INTEGER(10) UNSIGNED NOT NULL AUTO&#95;INCREMENT,
        parent&#95;id INTEGER(10) DEFAULT NULL,
        lft INTEGER(10) DEFAULT NULL,
        rght INTEGER(10) DEFAULT NULL,
        name VARCHAR(255) DEFAULT '',
        PRIMARY KEY  (id)
    );
    
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(1, 'My Categories', NULL, 1, 30);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(2, 'Fun', 1, 2, 15);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(3, 'Sport', 2, 3, 8);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(4, 'Surfing', 3, 4, 5);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(5, 'Extreme knitting', 3, 6, 7);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(6, 'Friends', 2, 9, 14);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(7, 'Gerald', 6, 10, 11);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(8, 'Gwendolyn', 6, 12, 13);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(9, 'Work', 1, 16, 29);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(10, 'Reports', 9, 17, 22);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(11, 'Annual', 10, 18, 19);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(12, 'Status', 10, 20, 21);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(13, 'Trips', 9, 23, 28);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(14, 'National', 13, 24, 25);
    INSERT INTO `categories` (`id`, `name`, `parent&#95;id`, `lft`, `rght`) VALUES(15, 'International', 13, 26, 27);
    

    Write the following in controller:

    <?php
    class CategoriesController extends AppController {
    
            var $name = 'Categories';
    
            function index() {
                    $this->data = $this->Category->generatetreelist(null, null, null, '&nbsp;&nbsp;&nbsp;');
                    debug ($this->data); die;
            }
    }
    ?>
    

    and in the model:

    <?php
    // app/models/category.php
    class Category extends AppModel {
        var $name = 'Category';
        var $actsAs = array('Tree');
    }
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: