doudu2404 2015-09-02 06:37
浏览 62
已采纳

我应该在MVC框架中使用Datatables后端插件吗?

I'm using an MVC PHP framework and I managed to write all models then I completed all backend staff. Later on on the frontend development to make everything easier and faster I found my self in-need to use Datatables plug-in, but this compels me to send response back in a specific format, so I wrote a plug-in that changes the returned values from models to the format needed in Datatables.

Example 1 - Model with my own plugin:

$this->myModel->setCostRange(50, 93);
$this->myModel->setCustomerId(123);
$results = $this->myModel->search();

// Then I use my plugin this way to change the response format
$results = $this->_buildGrid($results);

That was my solution, although Datatables provide many backend plugins to make queries instead of model, but as I believe this conflicts code separation principle and MVC logic.

Example 2 - no model use:

$g = new \Data\Grid("tableName");
$g->addColumn(
    "name",
    "Name"
 );
 $g->addColumn(
     "userEmail",
     "Email"
 );
 $g->render($_POST);

Any advises!

  • 写回答

1条回答 默认 最新

  • dongposhi8677 2015-09-02 06:55
    关注

    I think yes, you should call your plugin into your controller or where the business logic is written.You are using plugin to convert the data return from the database, you are just adding one layer over to your database model. It will be break the code separation logic of MVC if you write some core queries in Model or write conversion logic into the Model. But, If you think so, you can you use third party Libraries like doctrine or Active Records also, which will just need to be instantiated in your model and separates the login of data extraction layer and conversion logic from the model, there are lots of framework use those libraries to separate the data extraction logic with business logic and converts data from in proper format needed by your framework, on the basis of this you need to make some changes in you presentation layer also. Hope this will help you out :).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛