【问题标题】:how does TreeBehavior works in cakephp 3TreeBehavior 如何在 cakephp 3 中工作
【发布时间】:2015-08-11 05:42:07
【问题描述】:

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

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

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

这对添加新的根节点不起作用(我不明白为什么它是 10) 我有空桌子

请指导,我如何开始使用 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;

【问题讨论】:

  • I didn't understand why it's 10 there - 如果您的表是空的,您为什么要查找 id 为 10 的记录?您所写的问题与树的行为无关,它是“我如何保存记录” - 也许read the docs 考虑到这一点。

标签: cakephp tree cakephp-3.0 hierarchical-data mptt


【解决方案1】:

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

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

添加一个 id 为 10 的数据库条目,它会起作用。

所有 Cake 教程都假设您有一些正在使用的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2023-03-24
    • 2020-02-21
    相关资源
    最近更新 更多