假设我有一个 Web 应用程序,对于某些数据库表,我想将其数据作为数组/对象集合返回以显示在网页中,并作为 json 来构建 api。
我的问题是:我应该在我的模型中创建一个方法以将数据库中的数据作为 json 和其他方法以将数据返回为数组,还是应该只使用“getData”方法并在我的控制器中操作输出?
情况1:
模型:
function getDataFromDb(){
// query the db
// return as array/obj
}
function getDataAsJson(){
result = getDataFromDb();
// manipulate the result and return json object
}
案例2
模型:
function getDataFromDb(){
// query the db
// return as array/obj
}
控制器
result = getDataFromDB();
// create json data from the returned result