Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateData ($size, $depth, $parent) {
- $result = array();
- for ($i = 0; $i < $size; $i++) {
- $elem = new Elem;
- $elem->setData ($parent ? $parent->getData() . '.' . ($i+1) : '#'.($i+1));
- $elem->setChildren ($depth ? generateData($size, $depth-1, $elem) : array());
- $elem->setParent ($parent);
- $result[] = $elem;
- }
- return $result;
- }
- function insertTreeFHRecursive ($tree) {
- $qElem = "INSERT INTO `Elements` (`ID`, `Data`) VALUES (NULL, '%s')";
- $qHier = "INSERT INTO `ElementsHierarchy`(`ElemID`,`ParentID`,`Level`) VALUES (%s)";
- $qParents = "SELECT * FROM `ElementsHierarchy` WHERE `ElemID` = '%d'";
- foreach ($tree as $elem) {
- $query = sprintf($qElem, Db::escape($elem->getData()));
- $elem->setId(Db::execute($query));
- $parents = array();
- if ($elem->getParent()) {
- $parentId = $elem->getParent()->getId();
- $parentParents = Db::select(sprintf($qParents, $parentId));
- foreach ($parentParents as $k => $v) {
- $parents[] = join (',', array ($elem->getId(), $v['ParentID'] ? $v['ParentID'] : 'NULL', $v['Level']+1));
- }
- $parents[] = join (',', array ($elem->getId(), $parentId, 1));
- $parentsString = join ('), (', $parents);
- } else {
- $parentsString = join (',', array($elem->getId(), 'NULL', 1));
- }
- Db::execute(sprintf($qHier, $parentsString));
- if ($elem->getChildren()) {
- insertTreeRecursive($elem->getChildren());
- }
- }
- }
- function insertTreeMPRecursive ($tree) {
- $qElem = "INSERT INTO `ElementsMP` (`ID`, `Data`, `Path`) VALUES (NULL, '%s', %s)";
- $qParent = "SELECT `Path` FROM `ElementsMP` WHERE `ID` = '%d'";
- foreach ($tree as $elem) {
- if ($elem->getParent()) {
- $parentID = $elem->getParent()->getId();
- $parent = Db::select(sprintf($qParent, $parentID));
- if ($parent[0]['Path']) {
- $path = $parent[0]['Path'] . $parentID . '.';
- } else {
- $path = '.' . $parentID . '.';
- }
- $query = sprintf($qElem, $elem->getData(), "'$path'");
- $elem->setId(Db::execute($query));
- } else {
- $query = sprintf($qElem, $elem->getData(), 'NULL');
- $elem->setId(Db::execute($query));
- }
- if ($elem->getChildren()) {
- insertTreeMPRecursive($elem->getChildren());
- }
- }
- }
- function getTreeByParent_MP ($id) {
- $id = (int) $id;
- Db::select("SELECT SQL_NO_CACHE * FROM `ElementsMP` WHERE `Path` LIKE '%.$id.%'");
- }
- function getTreeByParent_FH ($id) {
- $id = (int) $id;
- Db::select("
- SELECT SQL_NO_CACHE `Elements`.*, `Parent`.`ParentID` FROM `Elements`
- INNER JOIN `ElementsHierarchy` as `Hier`
- ON `Hier`.`ElemID` = `Elements`.`ID`
- INNER JOIN `ElementsHierarchy` as `Parent`
- ON (`Parent`.`ElemID` = `Elements`.`ID` AND `Parent`.`Level` = 1)
- WHERE `Hier`.`ParentID` = $id OR `Elements`.`Id` = $id
- ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement