我们如何为我们从其他来源收到的数据插入嵌套集模型的'lft'和'rgt'值?

时间:2010-11-19 13:21:02

标签: php mysql twitter datasource nested-sets

我必须为我的Web应用程序管理一些分层数据。数据由四种类型的消息组成。我决定使用嵌套列表模型,因为数据中可以有任意数量的子节点。我需要存储的数据来自外部源,如何将值添加到'lft'和' 'rgt'字段用于插入mySQL。
EDITED 这就是我使用curl显示我的应用程序从Twitter获取的消息(时间轴)的方式。

foreach ($xml->entry as $status) {
       echo'<li>'.$status->content.'</li>';
   }

现在尽管直接打印,我还是需要将'$ status-&gt; content'的内容存储到数据库中,以便我可以操作那些在父子层中打印的内容。

1 个答案:

答案 0 :(得分:0)

一种可能性是编写一个递归函数或一个遍历要导入的数据集的过程。

一切都取决于其他来源的结构。如果它是一个经典的父id层次结构,那么在Java世界中你可以按照以下精神做一些事情:

public class Importer {

    public static db = new Database();

    public static void processOtherSource(ArrayList<Object> data, int lft) {
        int siblings = 0;
        for (Object element : data) {
            int children = 0;
            if (element.getClass().getName().equals("ArrayList")) {
                children = Importer.processOtherSource(element, lft+1, rgt);
            }
            int rgt = lft + children * 2 + 1;
            Importer.db.insert(element, lft, rgt);
            siblings++;
        }
        return siblings;
    }

    public void main(String[] args) {
        Importer.processOtherSource(dataToImport, 1);
    }

}