【问题标题】:Populate Model from a Query with a One-to-Many Relationship从具有一对多关系的查询中填充模型
【发布时间】:2017-01-18 04:11:47
【问题描述】:

我有两张桌子:

surveyTemplateHeader
surveyTemplateQuestions

我想做的是从两个表中获取信息:

var q = dao.getSurveyTemplateDetails( surveyTemplateID = ARGUMENTS.surveyTemplateID );

然后使用 Coldbox 使用查询数据填充我的模型 surveyTemplate

var surveyTemplateObj = populator.populateFromQuery( beanFactory.getInstance("surveyTemplate"), q );

这可行,然而,模型只填充了一个问题。我正在测试的调查模板有 三个 问题。

我尝试在我的模型中正确设置property name 以使用fieldtype="one-to-many",但这似乎没有什么不同。

property name="surveyTemplateQuestionID" fieldtype="one-to-many";

虽然 Coldbox 的模型文档总体上非常好,但我无法找到答案,这可能意味着我在实现这一点时偏离了轨道。

任何见解将不胜感激。

【问题讨论】:

    标签: coldfusion coldfusion-9 coldbox


    【解决方案1】:

    所以,我为此所做的就是将surveyTemplateQuestions 模型注入我的surveyTemplate 模型:

    property name="surveyTemplateQuestions" inject="surveyTemplateQuestions";
    

    然后我用问题查询设置surveyTemplateQuestions 属性:

    surveyTemplateObj.setSurveyTemplateQuestions(qQuestions);
    

    不完全是我想要的,但它现在有效。

    【讨论】:

      【解决方案2】:

      您可以循环查询以构建对象数组。 populateFromQuery 方法采用查询行号从查询中获取数据。

      var surveyTemplateObj = [];
      
      for(var row in q){
          ArrayAppend(surveyTemplateObj, populator.populateFromQuery( beanFactory.getInstance("surveyTemplate"), q , row.currentRow));
      }
      

      API 文档信息 http://apidocs.ortussolutions.com/coldbox/4.3.0/coldbox/system/core/dynamic/BeanPopulator.html#populateFromQuery()

      【讨论】:

      • 感谢您对萨娜的评论。我实际上是在调查模板列表页面的另一部分代码中执行此操作。不同之处在于我只需要标题表中的信息。我想让一个对象代表一个完整的调查模板,以及它的所有问题。如果我要遍历查询并创建一个问题对象数组,我是否能够以某种方式将其“合并”到带有标题信息的模板对象中?谢谢!
      猜你喜欢
      • 1970-01-01
      • 2017-08-12
      • 2018-10-08
      • 2014-10-23
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多