从具有一对多关系的查询中填充模型

时间:2017-01-16 22:07:51

标签: coldfusion coldfusion-9 coldbox

我有两张桌子:

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的整体模型文档非常好,但我无法找到答案,这可能意味着我在执行此操作时偏离了轨道。

任何见解都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

您可以循环查询以构建对象数组。 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()

答案 1 :(得分:0)

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

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

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

surveyTemplateObj.setSurveyTemplateQuestions(qQuestions);

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