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 :).

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

报告相同问题?