0

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;
Surjit Sidhu
  • 377
  • 4
  • 14
  • 1
    `I didn't understand why it's 10 there` - if your table is empty, why are you trying to find the record with id 10? Your question as written has nothing to do with the tree behavior it's "how can I save a record" - perhaps [read the docs](http://book.cakephp.org/3.0/en/orm/saving-data.html#inserting-data) with that in mind. – AD7six Aug 11 '15 at 06:05

1 Answers1

3

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.

Matt Stephens
  • 922
  • 1
  • 6
  • 24