-1

I do not understand what is best practice to get data from a database in a MVC framework. I have seen two scenario's:

  1. Do the database query in the Controller class

$feeds = Feedback::find()->orderBy('RAND()')->limit(4)->all();

  1. Do the database query in the Model class, and call this method from a Controller class

$categories = Category::getCategoriesList();

What is the correct way? Which tasks should I perform in a Controller class and which should I perform in a Model class?

iep
  • 601
  • 1
  • 8
  • 23
artem55555p
  • 107
  • 1
  • 6
  • You really should stop referring to table abstraction as "models". They are not. Model is a layer, that contains domain business logic. What you have there is just a piece of persistence layer. – tereško Nov 21 '17 at 20:08
  • Please provide the source of the 'watched how two different people do the site'. As @tereško pointed out `model` is an abstraction layer; typically data source abstraction. So a database table would have a corresponding 'model class'. – David J Eddy Nov 21 '17 at 20:49
  • why reputation of my question is -1? – artem55555p Nov 21 '17 at 20:50
  • @DavidJEddy that was not what I pointed out: https://stackoverflow.com/a/5864000/727208 – tereško Nov 21 '17 at 21:13
  • 1
    @tereško while an excellent q/a post (bookmarked it myself to assist in the future) Yii2 is not architected in this way. http://www.yiiframework.com/doc-2.0/guide-structure-models.html explaines that Views are UI element rendering, Controllers are business (domain) logic, and model/model classes are database abastractions. Your provided resource explains MVC in a way that is closer related to client frameworks such as Angular/React/et al. Neither is correct not incorrect; simply different implementations of the same abstraction pattern. Thanks again for the resource, it'll be handy for sure. – David J Eddy Nov 21 '17 at 22:17
  • 1
    @DavidJEddy even in what Yii tries to sell as MVC (it's actually completely unrelated to it) you would still benefit from creation of service layer to isolate the active record instances from the controller code. – tereško Nov 22 '17 at 11:21
  • @tereško Agree with you on both point! Yii uses MVC in name only as a way to explain the layers of abstraction within the framework. Service layers are super beneficial in many instances. – David J Eddy Nov 22 '17 at 15:29

3 Answers3

0

Define custom query methods in the model. Call those model methods in the controller when appropriate to retrieve the desired data. Yii2 uses a activerecord ORM design pattern; this means each table typically has a model class that represents a table.

David J Eddy
  • 1,999
  • 1
  • 19
  • 37
0

if you want to get data directly with out data providers (arrraydataprovider,activedataprovider etc ) you can use First Method is best way.if you want to get data and return that data to any widgets like (grideview,listview etc) you can use second method

-1

There is much debate where to put your business logic in MVC.

Generally speaking, in MVC architectures one separates at least database logic (Model) from your Controller.

Database specific queries are done in the model.

Methods in your Controller handle requests (resolve routes) and create the proper response (a view or plain data).

Example

If you have a route/url /feedback you would have a 'feedback()' method in your FeedbackController.

class FeedbackController {

  use Feedback; 

  public function feedback(){
     return Feedback::list();
  }
}

In your Model (Feedback) you now need a method list().

class Feedback extends Model {

  public static function list(){
     return ( your_database_specific_query_which_returns_a_list_of_records );
  }

}
iep
  • 601
  • 1
  • 8
  • 23
  • Looks like you do not understand the difference between "business logic" and "database logic". – tereško Nov 21 '17 at 20:02
  • This [question and its answers](https://stackoverflow.com/questions/235233/asp-net-mvc-should-business-logic-exist-in-controllers) might be helpful. – iep Nov 22 '17 at 09:05
  • Why downvote this answer? See [https://stackoverflow.com/questions/47420969/explain-the-selection-of-data-from-the-database-mvc#comment81802147_47420969](the remark of David) – iep Nov 22 '17 at 10:55