Coldfusion查询到对象树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Coldfusion查询到对象树相关的知识,希望对你有一定的参考价值。

我需要将SQL查询转换为令人讨厌的对象树。

基本上我需要有这种树

[
    id_record,
    mSap,
    process_rev,
    process: [
        {
            mpProcess,
            mpOrder,
            bom: [
                {
                    mbItem,
                    mbQuantity,
                    mbYield,
                    alt: [
                        {
                            maFatherItem,
                            maAltItem
                        }
                    ]
                }
            ]
        }
    ]
]

这是我需要转换的查询结果in JSON format.。我想我可以在ES6 javascript中做到这一点,但我需要使用Coldfusion。我也可以阅读php,所以也许我可以将它转换为Coldfusion。它太复杂了,我不知道怎么做。

这是询问所有人的查询:

SELECT a.id_record, a.mSap, 
       b.process_rev, b.mpOrder, b.mpProcess, b.bom_rev, b.mbITem, b.mbQuantity, b.mbYield,  b.maFatherItem, b.maAltItem 
  FROM tbl_model a 
  LEFT JOIN ( 
     SELECT c.mpModel AS model, d.process_rev, c.mpOrder, c.mpProcess, e.bom_rev, e.mbItem, e.mbQuantity, e.mbYield, e.maFatherItem, e.maAltItem   
       FROM tbl_model_process c 
      INNER JOIN ( 
          SELECT mrModel, MAX(mrRevision) AS process_rev 
            FROM tbl_model_revision 
           WHERE mrApproved = 2 
             AND mrType = 0 
             AND mrActive = 1 
           GROUP BY mrModel
            ) d 
              ON c.mpRevision = d.process_rev 
             AND c.mpModel = d.mrModel 
         LEFT JOIN ( 
            SELECT f.mbModel, f.mbRevision AS bom_rev, f.mbProcess, f.mbItem, f.mbQuantity, f.mbYield, h.maFatherItem, h.maAltItem 
              FROM tbl_model_bom f 
             INNER JOIN ( 
                 SELECT mrModel, MAX(mrRevision) AS bom_rev 
                   FROM tbl_model_revision 
                  WHERE mrApproved = 2 
                    AND mrType = 1 
                    AND mrActive = 1
                  GROUP BY mrModel
                  ) g 
                    ON f.mbModel = g.mrModel 
                   AND f.mbRevision = g.bom_rev 
               LEFT JOIN ( 
                  SELECT maModel, maProcess, maRevision, maFatherItem, maAltItem
                    FROM tbl_model_alt
                    ) h 
                      ON f.mbModel = h.maModel 
                     AND f.mbProcess = h.maProcess 
                     AND f.mbItem = h.maFatherItem 
                     AND f.mbRevision = h.maRevision
          ) e 
            ON c.mpModel = e.mbModel 
           AND c.mpProcess = e.mbProcess 
    ) b 
   ON b.model = a.id_record 
WHERE a.mActive = 1 
  AND a.mSap IN (:models) 
ORDER BY a.id_record, b.process_rev, b.mpOrder, b.mpProcess 
答案

根据您使用的cf版本,按进程,bom,alt对查询进行分组,然后使用cfoutput或cf10 + loop的group属性并构建json输出。

另一答案

查询是ColdFusion变量吗?你能简单地将它发送到serializeJSON()吗?

<cfquery datasource="dsn" name="qry">
SELECT * FROM table
</cfquery>
<cfoutput>#serializeJSON(qry)#</cfoutput>

以上是关于Coldfusion查询到对象树的主要内容,如果未能解决你的问题,请参考以下文章

div元素到coldfusion变量

我可以在 ColdFusion 中按索引获取查询行吗?

ColdFusion中的查询优化

调用 .NET Web 服务的 ColdFusion 代码

Coldfusion / sql查询以查找下一个ID [重复]

Coldfusion / Lucee - 使用一个查询循环遍历3D数组以使用多个插入插入到数据库中