<?php
class Sqltree {
private $FlatTable, $paths;
public function __construct($FlatTable) {
$this->FlatTable = $FlatTable;
}
public function generateTable($tree) {
$this->paths = array();
$this->path_recursion($tree, array());
print_r($this->paths);
}
private function path_recursion($tree, $parents) {
//Path erzeugen
$this->paths[] = array($tree, $tree, 0);
foreach($parents as $parent) {
$this->paths[] = array($parent['id'], $tree['id'], $parents['path_length']);
$parents['path_length']++;
}
if(is_array($tree['children'])) {
$parents = array('id' => $tree['id'], 'path_length' => 1);
foreach($tree['children'] as $child) {
$this->path_recursion($child, $parents);
}
}
}
public static function CreateTable($FlatTable) {
$sql = "CREATE TABLE ClosureTable (
ancestor_id INT NOT NULL REFERENCES $FlatTable(id),
descendant_id INT NOT NULL REFERENCES $FlatTable(id),
path_length INT,
PRIMARY KEY (ancestor_id, descendant_id)
);";
sql::create($sql);
}
}
$tree = array('id' => 1, 'children' => array(array('id' => 2, 'children' => array('id' => 4, 'children' => false)), array('id' => 3, 'children' => false)));
print_r($tree);
$sqltree = new SqlTree('asd');
$sqltree->generateTable($tree);
?>
preferences:
41.14 ms | 402 KiB | 5 Q