可以说我有一个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