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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 271
    Comment on it

    When you want to store a heirarchical data in the database table, then we use the tree structure in cakephp.

    For e.g : When you need to form a heirarchy of categories & subcategories like:-

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

    For creating tree structure in cakephp, we need to create 3 columns in the database table namely, 

    1. Parent -> Default field name is parent_id, which stores the id of the parent object.
    2. Left-> Default fieldname is lft, to store left value of current row.
    3. Right-> Default fieldname is rght, to store the right value of the current row.

    In database, create the following table and insert data in that table ;-

    CREATE TABLE categories (
        id INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        parent_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_id`, `lft`, `rght`)
    VALUES
      (1, 'My Categories', NULL, 1, 30);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (2, 'Fun', 1, 2, 15);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (3, 'Sport', 2, 3, 8);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (4, 'Surfing', 3, 4, 5);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (5, 'Extreme knitting', 3, 6, 7);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (6, 'Friends', 2, 9, 14);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (7, 'Gerald', 6, 10, 11);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (8, 'Gwendolyn', 6, 12, 13);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (9, 'Work', 1, 16, 29);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (10, 'Reports', 9, 17, 22);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (11, 'Annual', 10, 18, 19);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (12, 'Status', 10, 20, 21);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (13, 'Trips', 9, 23, 28);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (14, 'National', 13, 24, 25);
    INSERT INTO
      `categories` (`id`, `name`, `parent_id`, `lft`, `rght`)
    VALUES
      (15, 'International', 13, 26, 27);

    For creating tree structure, we have to make two views namely, add.ctp & index.ctp.

    In add.ctp, add the following code :-

    <?php
    
    echo $this->Html->link('Back',array('action'=>'index', 'method'=>'post'));
    
    echo $this->Form->create('Category',array('controller' => 'categories'));
     
    echo $this->Form->input('parent_id',array( 'label'=>'Parent'));
    
    echo $this->Form->end('Add');
    
    ?>
    

    In index.ctp, add the following code :-

    <?php
    
    echo $this->Html->link('Add Category',array('action'=>'add'));
    
    echo '<table>';
    
    foreach($userlist as $key=>$value){
    	echo '<tr><td>';
    		echo $value;
    	echo '</td></tr>';
    }
    echo '</table>';
    ?>

    In Controller.php, add the following code:-

    <?php
    class UsersController extends AppController {
    
     var $uses = array('User', 'Category');
    
     function index() {
        $data = $this->Category->generateTreeList(null, null, null, '&nbsp;&nbsp;&nbsp;');
            //print_r($data);die;
        $this->set('userlist',$data);
    }
    
    
    function add() {
        if (!empty($this->data)) {
            if($this->request->post)
            $this->Category->save($this->data);
            $this->redirect(array('action'=>'index'));
        } else {
            $parents[0] = "[ --Select Category-- ]";
            $categorylist = $this->Category->generateTreeList(null,null,null,"");
            if($categorylist) {
                foreach ($categorylist as $key=>$value)
                    $parents[$key] = $value;
            }
            $this->set(compact('parents'));
        }
    }
    
    
    
    }
    
    
    ?>
    

    In model.php, add the following code :-

    <?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: