【发布时间】:2011-06-21 12:01:14
【问题描述】:
我计划使用 orm 学说提供的嵌套集功能来拥有线程化 cmets。换句话说,我想将多个独立的树存储在一个表中。在我的具体情况下,会有一些实体“新闻”有很多“评论”。
News:
columns:
content: clob
Comment:
actAs:
NestedSet:
hasManyRoots: true
rootColumnName: root_id
columns:
benutzer_id: { type: integer, notnull: true }
ref_id: { type: integer, notnull: true }
content: { type: clob, notnull: true}
relations:
News:
class: News
local: ref_id
foreign: id
我如何告诉教义使用comment.ref_id 作为鉴别器列?因此,只有具有相同 ref_id 的节点才与一棵树相关。目前所有的树操作都会影响存储在评论表中的所有节点。希望只有具有给定列名(“ref_id”)的节点才能充当一棵树。
更新 为了解决这个问题,我正在考虑产生一种在一个表中拥有许多 hasManyRoots-trees 的方法。 要加载一棵树,必须像这样创建一棵树:
$category->$treeObject = Doctrine_Core::getTable('Category')->getTree('ref_id',12);
所有的树操作操作都应该包括“WHERE ref_id=12 AND ...”。在我的例子中,你会为新闻#12 操作 cmets-tree。因此,数据库更新语句会更少。由于 ref_id 与新闻有关,因此 ref_id 上已经有一个索引,因此它应该运行得相当快。
最终解决方案 - 不是问题的一部分
经过大量讨论和沉思,我想出了以下架构。它包括减少评论表中的列(踢出 ref_id,root_id 现在引用根评论而不是新闻了)。
News:
columns:
content: clob
comment_root_id: { type: integer, notnull: false }
relations:
CommentRoot:
class: Comment
local: comment_root_id
type: one
Comment:
actAs:
NestedSet:
hasManyRoots: true
columns:
content: { type: clob, notnull: true}
我认为这样更干净。创建新闻需要创建一个虚拟根节点。
$treeObject = Doctrine_Core::getTable('Comment')->getTree();
$root = new Comment();
$root ->content = 'root';
$root->save();
$root = $treeObject->createRoot($root);
$news->setCommentRoot($root);
$news->save();
最后,您可以在查询“新闻”时使用左连接来获取根评论以告诉您有多少孩子。
对于性能问题,您可能希望在 root_id 列上手动放置索引。完成。
【问题讨论】: