Should the MVC approach separate my model functions, or combine as much as possible in this case?

Should you separate model functions even if they extract data fields?

Say I have an article model that retrieves articles from a database.

I have a function getUserArticles($id)that receives user articles. I have a function getAllArticles($offset, $limit)that gets a list of articles.

Should I leave the two functions separate from each other or somehow combine them.

The reason I'm asking is because if I were to change, add or remove a field from a query, I would have to make changes in each function. Therefore, if I change my mind and decide that I no longer want to show time_addedfor each article, I will have to remove it from each function.

+3
source share
2 answers

It’s better to expose two different functions to the user of the API, but internally you can use the same code for both, if you think that better code reuse will be used.

For example: you define a generic getArticles (for internal use only)

   articles getArticles($userId, $offset, $limit) {
      // do searching while handling the different cases if $userId is set or not and the same for the offsets
    }

then you define your public functions like

   articles getUserArticles($id){
     return getArticles($id, -1,-1);
   }

  articles getAllArticles($offset, $limit) {
    return getArticles(-1, $offset, $limit);
  }

NOTE. do this only if you think that there is a common code between the two functions. If there is no generic code, save them separately and there is no need for the generic getArticles function that I suggested.

I don't know if the above is valid PHP code, but I think the idea is clear.

+1
source

. . , . , . , .

0

Source: https://habr.com/ru/post/1728996/


All Articles