dsgtew3241 2014-11-14 10:39
浏览 80

在源数据根本没有在层次结构中组织时创建递归树(PHP + SQL Server)

Despite I've checked a lot of StackOverflow questions, as well as Google, I can't find any solution for this. (any solution which I can understand and be able to do).

The status quo

  1. I cannot alter source database (but I would if there's not any other way).
  2. Items of the hierarchy are into separated tables. (I have not a master hierarchy table).
  3. If I query one of those tables, I have a relation of an item and its parent, but between diferent tables, those items doesn't share its parenting.

Relation to achieve:

  • Country
    • Region
      • City
        • And so on ...

My starting point

Source data is in the following tables (depicted as table => columns)

country_table => id
region_table => id, country_id
city_table => id, region_id
... and so on ...

But when I query&join these tables, they don't share a "master" parent in any way. D'oh!

Solutions I've thought so far

  • Screw the "cannot alter source database" and build a proper hierarchy table (following the guides from an answer to this question: What is the most efficient/elegant way to parse a flat table into a tree?
  • Worst never-ever do solution (if it can be called a solution at all): Hardcode the tree hierarchy, parenting all I can in each table and then parenting table by table.Con: Unmaintenable code, the deeper the hierarchy goes, the longest the lines of code are. And if hierarchy changes you're screwed.

EDIT: Final decision (and solution)

  • I didn't thought about converting that array into an XML Object (using PHP's SimpleXMLElement).

Code:

function buildLocationTree()
{ 
    //We retrieve raw data from model (Laravelish style)
    $aLocations = WhateverModel::getLocations();

    $oXML = new SimpleXMLElement('<xml/>');

    //I add a root node called 'result'
    $oResult = $oXML->addChild('result');

    foreach ($aLocations as $oLocation)
    {   
        if (! ($oXML->xpath("//country[@id=" . $oLocation->CountryId . "]")))
        {
            $oCountry = $oResult->addChild('country', $oLocation->CountryName);
            $oCountry->addAttribute('id', $oLocation->CountryId );
        }

        if (! ($oXML->xpath("//region[@id=" . $oLocation->RegionId. "]")))
        {
            $oRegion = $oCountry->addChild('region', $oLocation->RegionName);
            $oRegion->addAttribute('id', $oLocation->RegionId );
        }

        //And so on... build many structures 'search in nodes + add child' as deep levels
     }

     return $oXML;
}

Then do whatever you need with the XML object... jsonize it, build a file...

I'd be nice to have some feedback or listen your thoughts about this solution! Is an acceptable one? Risky or prone to errors?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于无人驾驶的航向角
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退