TreeBehavior 如何在 cakephp 3 中工作

how does TreeBehavior works in cakephp 3

我正在关注这个 http://book.cakephp.org/3.0/en/orm/behaviors/tree.html 制作简单的类别树。

您可以通过将“parent_id”列设置为空来使节点成为树中的根:

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

这对添加新的根节点不起作用(我不明白为什么那里有 10 个) 我有空 table

请指导我如何开始使用 root 并向其添加子节点

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;

树行为背后的想法是将分层数据保存到数据库中。

如果您没有数据,那么 $categoriesTable->get(10); 将失败,因为没有 ID 为 10 的条目。

添加 ID 为 10 的数据库条目,它将起作用。

蛋糕教程的所有内容都假设您有一些正在使用的数据。