2

One of the things that I don't fully understand about the MVC model is loading multiple data tables from the model.

Let's say we have a Model called user.


public class User{

protected $username; 
protected $email;
protected $id;

public function setUsername($name, $id) { 
$db->set->name($name)
$db->set->id($id)

} 

public function getUsernameById($id) { 
return $db->get->name($id); 

} 

As far as I've learned, the model is equivalent to a single row in the database (called user).

Now I have written two functions. The first is for setting the username and the other is for retrieving the username through an ID.

Suppose you want to retrieve all users. Let's say through the getAllUsers() method. This method does not fit on the Model as it is not a single object.

Now I understand that for example, you can call this method (function) in your controller itself. But where can you define this method (function)? Since you don't do this on the model.

I also like to hear it if I am wrong :)

tereško
  • 58,060
  • 25
  • 98
  • 150
Daansk44
  • 497
  • 2
  • 4
  • 21
  • You implement those as class methods typically. Look at the design of other existing ORMs like [RedBeanPHP](https://redbeanphp.com/), [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) and [Eloquent](https://laravel.com/docs/master/eloquent) – tadman Apr 10 '19 at 17:06
  • Do you want to implement an ORM-like solution - where your _models_ would try to mirror a specific db tables structure and constrains? Or are you trying to build objects (your _models_) having the ability to directly execute sql queries in their methods? – PajuranCodes Apr 10 '19 at 17:52
  • Hey Dakis. Thanks for your comment. I am more concerned with the idea behind creating the MVC model (and linking it to MySql or another database). When I go to look at laravel, you have eloquently acted the majority of your queries. That's great, just suppose you set up an MVC model yourself, so it's useful to know the logic behind it (and I'm still getting a bit stuck). – Daansk44 Apr 10 '19 at 18:03
  • You are welcome. Sorry, but your 2nd and 3rd phrase are a bit confusing to me, since I'm not a native english speaker. From them, I understand that you want to implement a "classical" model layer, not ORM-like and without using an ORM library at all. Like I asked in the 2nd question of my previous comment. Am I right? _P.S._: In general, you have to prepend a specific user name with the "@" character (like: @dakis), so that the SO user is beeing notified about your comment. – PajuranCodes Apr 10 '19 at 18:15
  • @dakis Well yes I like it more because I want to understand the logic behind it better. When you look at laravel, a lot of things are done for you. And yes I want to do this without ORM, probably in the end. Are there examples of very simple (no robust) frameworks so that I can look at the structure better? – Daansk44 Apr 10 '19 at 18:22

1 Answers1

1

Basically your definition is correct:

Model objects hold data and define the logic for manipulating that data. For example, a Student object in the Basic sample application is a model object. It holds data describing facts about the object like the first and last name of the student and has methods that can access and change this data (getters & setters). Model objects are not directly displayed. They often are reusable, distributed, persistent and portable to a variety of platforms.

(from: http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/mvc.html)

So where to put the getAllUsers() function in case you do not just put it into the model?

Well first of all when we are talking about the term "model" we do not always mean model. In MVC the model (M) contains all of the business logic. This means it contains the domain model which is the model you are meaning in this question. But it also contains the service layer.

The service layer usually consists of various classes which provide functionalities like for instance getAllUsers() or getAllUsersWhichOwnAGreenTruck().

Here is a great graphic demonstrating this:

MVC Layers

(Source: Domain Model and Service Layer patterns in P of EAA)

In modern applications e.g. built with the Laravel PHP Framework you usually generate the database as well as the model and the database mapping/communication automatically (Example Eloquent ORM: https://laravel.com/docs/5.8/eloquent). But you start implementing complex application logic within the service layer.

Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • Thank you @blackbam for the reply :), Is there a very simple functional example in PHP somewhere? – Daansk44 Apr 10 '19 at 18:23
  • Similar very detailed first answer: https://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc – Blackbam Apr 10 '19 at 19:07
  • 1
    Ans basically you can simply add a class UserService "above" the User and here you go, dimple example :-) – Blackbam Apr 10 '19 at 19:07
  • @tereško Modern is a relative Term, one could also call Symfony modern or electricity modern ^^ though I do not think Laravel projects are unmaintainable which PHP framework would you think is recommendable? – Blackbam Apr 11 '19 at 12:27