Php oop MVC design - the right architecture for a data editing application

Now that I have read a huge number of posts, articles, questions and answers on OOP, MVC, and design patterns, I still have questions about how best to build what I want to build.

My little structure is built in MVC style. It uses smarty as a viewer, and I have a class configured as a controller that is called from a URL.

Now that I think I'm lost, is in the model part. I could mix models and classes / objects with many (or with small ones). A.

Anyway, an example. When the goal is to get a list of users who are in my database:

the application is called, for example. "users / list" Then the controller starts a list of functions, which opens an instance of the "user" class and requests this class to retrieve the list from the table. as soon as it returns to the controller, the controller pushes it towards the viewer, assigning a result set (array) to the template and setting the template. Then the user clicks on a row in the table that indicates that the controller launches "user / editing", for example - which in turn creates a form and fills it with user data for editing.

so far so good.

right now I have everything that is combined in one user class, so the class will have the create, getMeAListOfUsers function, update, etc. and properties like hairType and noseSize. But the correct oop design would like me to separate the "user" (with properties such as username, big nose, curly hair) from the "getme user list", which would be more like a "user manager class".

If I implemented a user manager class, what should it look like then? should it be an object (cannot really compare it with the real world) or should it be a class with just public functions so that it more or less looks like a set of functions.

Should it return an array of found records (for example: array([0]=>array("firstname"=>"dirk", "lastname"=>"diggler")) or should it return an array of objects.

All this still confuses me a bit, and I wonder if anyone can give me a little idea of ​​how to make the approach the best way.

+4
source share
3 answers

I am not an expert on this, but recently done almost the same thing. The way I installed it is that I have one class for several lines ( Users ) and one class for one line ( User ). A "row of several rows" is basically a collection of (static) functions, and they are used to extract rows from a table, for example:

 $fiveLatestUsers = Users::getByDate(5); 

And this returns an array of User objects. Each user object has methods for retrieving fields in a table (for example, $user->getUsername() or $user->getEmail() , etc.). I just returned an associative array, but then you came across situations where you want to change the data before returning it, and that where a class with methods for each field makes a lot of sense.

Edit: The User object also has methods for updating and deleting the current line;

 $user->setUsername('Gandalf'); $user->save(); $user->delete(); 
+1
source

The level of abstraction needed for processing and data (Business Logic) depends on your needs. For example, for an application with transactional scripts (which is probably the case with your design), the class that you describe that retrieves and updates data from the database sounds to me.

You can generalize things a little more using Table Data Gateway , Row Data Gateway or Active Record .

If you feel like you are duplicating a lot of code in transactional scenarios, you can create your own domain model using a Data Mapper . However, I would not blindly do this from the very beginning, because it requires a lot more code. It is also unreasonable to write the Data Mapper yourself, but to use an existing component for this. Doctrine is such a component in PHP.

Another existing component of ORM (Object Relational Mapper) is Propel , which provides active records.

If you're just looking for a quick way to query your database, you can find NotORM inspiring.


You can find the patterns in italics in

which lists all the templates in the Enterprise Application Architecture Templates book.

+6
source

Another alternative to Doctrine and Propel is PHP Activerecords .

The Doctrine and Prophet are truly powerful beasts. If you are doing a small project, I think you are better off with something easier.

In addition, when it comes to third-party solutions, there are many MVC infrastructures for PHP, such as: Kohana, Codeigniter, CakePHP, Zend (of course) ...

They all have their own ORM implementations, usually lighter alternatives.

For the Kohana framework, there is also an Auto modeler , which is supposedly very light.

Personally, I use Doctrine, but this is a huge project. If I were to do something less, I would rather go with a lighter alternative.

+1
source

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


All Articles