Search code examples
phpmysqltwitterdatasourcenested-sets

How we insert 'lft' and 'rgt' values for nested-set model for a data that we are receiving from other source?


I have to manage some hierarchical data for my web application. The data consist of messages of four types. I decided to use nested-list model because there can be arbitrary number of child nodes in the data. The data that I need to store is coming from an external source, how do I add values to the 'lft' & 'rgt' fields for it to be inserted into mySQL.
EDITED This is how I display messages(timeline) that my application fetches from Twitter using curl.

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

Now in spite of direct printing, I need to store the contents of '$status->content' into database so that I can manipulate those i.e print in a parent-child hierachy.


Solution

  • One possibility is to write a recursive function or a procedure that goes through the data set to be imported.

    Everything depends on the structure of the other source. If it's a classical parent id hierarchy, then in the Java world you could do something in the spirit of:

    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);
        }
    
    }