Search code examples
cakephptreecakephp-3.0hierarchical-datamptt

how does TreeBehavior works in cakephp 3


I'm following this http://book.cakephp.org/3.0/en/orm/behaviors/tree.html to make simple category tree.

You can make a node a root in the tree by setting the ‘parent_id’ column to null:

$aCategory = $categoriesTable->get(10);
$aCategory->parent_id = null;
$categoriesTable->save($aCategory);

this does not work to add new root node (I didn't understand why it's 10 there) I'm have empty table

please guide, me how to get started with root and the add child nodes to it

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `category_slug` varchar(250) DEFAULT NULL,
  `category_name` varchar(250) DEFAULT NULL,
  `lft` int(11) DEFAULT NULL,
  `rght` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Solution

  • The idea behind the Tree Behaviour is to save hierarchical data into the DB.

    If you have no data then $categoriesTable->get(10); will fail as there is no entry with the id 10.

    Add a DB entry with the id 10 and it will work.

    All of the Cake Tutorial assume you have some data you are working with.