r/PHPhelp • u/Itchy-Mycologist939 • Oct 28 '24
Confused between Models and Data Transfer Object (DTO)
I'm learning PHP and trying to log activities of the user or system for auditing purposes.
I'm having a hard time understanding the MVC framework when it comes to Models and DTOs.
I'm capturing a few things as an example:
- user or system
- action taken
- date and timeMy model currently looks something like:
public function getUser()
{
return $this->user;
}
public function setUser(string $user)
{
$this->user = $user;
}I then have another class that logs the user, action, and timestamp to a MySQL database.
Am I supposed to call the Model to log this information by adding another method like
public function log()
{
$this->db->insert($this->getUser);
}so my logging class then looks like
public function logAction($event)
{
$this->event = new EventModel();
$this->event->setUser('Michael');
$this->event->log();
}or do I create another class that handles the logging to database specifically - like a service or handler?
4
u/universalpsykopath Oct 28 '24
A DTO only has data, and whatever methods you need to get and set that data.
A Model, on the other hand, has both data and behaviour. So take this example
class User {
public function __construct(
private readonly string $username,
private string $hashedPassword
private readonly \DatetimeInterface $createdAt,
private bool $isActivated = false,
private bool $isBanned = false
) {}
public static function create(
string $username,
string $hashedPassword
) : User {
return new static(
$username,
$hashedPassword,
new \DateTime()
);
}
public function canLogIn(): bool {
return $this->isActivated === true && $this-> isBanned === false;
}
}This example might seem like a DTO, but it isn't. The reason it isn't is that, by the way it's written, it's enforcing it's own business logic. It has behaviour in other words.
1: In order for a user to be useful, they need to have a name and a password, so I won't let you make a user object without them.
Similarly, with my static create method, I'm enforcing the need for a created At date, but providing it myself, for convenience.
2: My other business rules are that when you create a new user, they will be neither activated nor banned, so my defaults reflect that.
3: I could have a separate canLogin property, but whether a user can attempt to login (before a password check) depends on whether the account they're trying to log into is activated and/or banned.
If I created that property, it opens up the possibility of breaking my own business rules (a user could be marked as canLogIn = false even if they're activated and not banned).
So rather than do that, my method reflects my business rules.
tl;dr: DTOs are just for data. Models are for business rules and data. (Apologies for formatting, writing on my phone)
1
u/equilni Oct 29 '24
A Model, on the other hand, has both data and behaviour.
A model can be seen as a layer, not a singular class. What you are describing can be an Entity.
For me, your example is 2 separate classes - one for the data and one for the rules. The rules can get complex, so keeping them separated is ideal.
I could see where the example could be used - during the registration process - if you wanted to log the user attempting to log in. But I think
createdAt,isActivated,isBanned,canLogIn()may not be needed yet and can be deferred.set temp user: $tempUser = new UserDTO(username, email, password) new UserProfile( user: $tempUser, isActivated: false, isBanned: true ); on valid registration: $validatedUser = new UserDTO(username, email) new UserProfile( user: $validatedUser, isActivated: false, isBanned: false ); on activation: $user = $service->getUserByEmail(): UserDTO(username, email) new UserProfile( user: $user, isActivated: true, isBanned: false );
3
u/latro666 Oct 28 '24 edited Oct 28 '24
This is a software design thing. There will be 1000 ways to achieve this sort of outcome and there will always be pros and cons to each.
You want it so it's as reusable and easy to spread across other classes as possible.
Iv done something similar and it's a simple logger class which is brought in via dependency injection mainly in controllers.
You pass an action string, data and who did it and it saves it.
E.g. $this->logger->log("updateprofile",$data,$whodidit);
Frameworks like laravel have clever middleware and service stuff to make this sort of thing much easier.
3
u/darkhorsehance Oct 29 '24
Martin Fowler introduced DTO’s in his book https://martinfowler.com/books/eaa.html
A DTO is useful for transferring data between systems. They should only be data and should encapsulate their own serialization logic/config.
1
u/counteruroffer Oct 30 '24
Here's where I get confused. Say I have a validation class for users. A user updates their password. The password and other information is sent to the user validation class, the validation class validates the password meets the rule requirements and adds it to the Validation Results DTO.
Now there's a problem, the password is in the validation result DTO but it is unencrypted.
So the validation result dto is returned to say User Update Service.
Is it acceptable for User Update Service or even UserUpdateHandler to encrypt the password and save it in the DTO?
Now, say the encryption service also generates a pepper. Can I also add that to validation result dto?
I hope my question makes sense. Basically, is it okay to modify the DTO with known valid data because following the design pattern of the application, validation result is what is saved in the application.
3
u/batty3108 Oct 29 '24
As others have said, DTOs are purely for moving data around - they shouldn't be able to do anything with this data.
A really common use case for them is to transform an HTTP response.
A JSON response string usually gets decoded into an array or a stdClass, which is probably fine if you don't need to do much with it.
But if you need to do stuff with different data points from this response, and especially if you can't guarantee the presence of some array keys, then a DTO can make it a lot easier to work with.
Simply parsing the response into your DTO can handle a lot of the validation - e.g., if a particular key isn't present, or the value is the wrong type, trying to create the object will throw an exception that you can catch and handle.
And once created, you can use $dto->property, confident that it exists and is of the right type.
2
u/lOo_ol Oct 28 '24
In short:
Model: handles logic, bridge with database, among other things.
View: handles what is shown to the user (e.g. HTML if you're working on a website)
Controller: handles user action, calls models and views (and their respective methods) when needed.
So log entries should be handled by a Log Model, which would include insert() and retrieve() methods for instance. Those methods would be called accordingly by your controller.
DTOs are not necessary to satisfy an MVC design pattern. They are simply objects that carry data. Don't bother with those just yet. Try to understand design patterns and best practices first.
1
u/equilni Oct 29 '24
I'm having a hard time understanding the MVC framework when it comes to Models and DTOs.
MVC is a design pattern for software and framework can adopt MVC as part of their design. MVC are layers of that design pattern.
Models often times are confused with just the database, but in actually, it's your application without the output (view) or the i/o flow (controller). ADR (Action Domain Responder) is a better term for what is going on as the Domain can be tied to Domain Driven Design (a more advanced design pattern).
DTOs are just as they are, data transfer objects. These are great as they can enforce types (unlike arrays) and can be typehinted against when passing them (ie you know what you are getting, vs an array). DTO's can be anywhere in your application.
Example:
class UserDTO {
public function __construct( // PHP 8 constructor
// PHP 8.1
public readonly int $id,
public readonly string $username,
public readonly string $email,
) {
}
}
class UserGateway {
public function __construct(
private PDO $pdo
){
}
public function getUserById(int $id): UserDTO
{
// usual stuff
return new UserDTO(
id: // id
username: // username
email: // email
)
}
}
class UserService {
public function __construct(
private UserGateway $userGateway
){
}
// Called by Controller
public function getUserInfo(int $id): UserDTO
{
return $this->userGateway->getUserById($id);
}
}do I create another class that handles the logging to database specifically - like a service or handler?
Yes. Let the logger log but then it's up to you to keep the data store separate (it would be recommended) as this can be a file, database, whatever.
2
u/akkruse Oct 29 '24
You're getting into some areas where details and context can be important, and this can unfortunately complicate things quite a bit as far as research/learning is concerned (as evidenced by some of the responses here). The one thing mentioned here that pretty much holds true in all scenarios is what a DTO is/isn't, which is why the responses on this particular topic are pretty consistent here. These are "dumb" objects that represent the "shape" of data and are used for moving data between layers. In other words, these are a collection of related properties under a single class name.
Beyond that, a lot depends on details, context, and your specific setup. Take u/lOo_ol's response, for example. I agree with the definition of "view" and "controller", but not so much "model"... at least not initially. My gut reaction is things like "handles logic, bridge with database" and "log entries should be handled by a Log Model" are not things that should be handled by the model used in views. Reading further, however, it starts to make more sense when he says "Don't bother with those [DTOs] just yet. Try to understand design patterns and best practices first". This is when I realized that my gut response would have inadvertently been influenced by some other best practices that are good to follow but start going beyond "pure" MVC that you specifically asked about and are trying to learn. If your app really is just "pure" MVC, then I agree with u/lOo_ol's feedback for the most part (although data access might be better in controllers than in models). Either way, this highlights two important things: responses are dependent on the information provided (and leaving out details for the sake of simplicity could mean ommitting important details), and the people responding might be biased by their own practices to the point where it's second nature and they don't realize how it might complicate things when you're trying to learn one specific thing.
I'll try to wrap this up... when I say "pure" MVC, I mean an app where pretty much everything happens in a model, view, or controller. By contrast, many apps will use MVC as just one of several different parts of the app. For example, MVC might be used in the presentation layer, but not in a separate business logic/app layer or a data layer. There could also be two separate "instances" of MVC happening, one at the server's presentation layer (controllers using models and views to return a response) and one at the client (ex. a JavaScript framework that uses MVC for dynamic content). If your app has a data layer, then data access absolutely should not be happening directly in the presentation layer (and the models used by views should not have this type of logic). When you have multiple layers like this, then this is where DTOs start to become more useful. These multiple layers can also complicate the concept of a "model" like you're seeing. A model in the context of MVC in a presentation layer refers to the "thing" used by views to display data to the user (sometimes called "view models"). Or in a data layer it could refer to a "thing" that represents what might ultimately be a record in a specific database table (sometimes called "entities"). Or in DDD it could refer to a "thing" that represents logic carried out using multiple entities ("domain model"). The term "model" in particular can be very ambigious, which is why I usually avoid it in favor of something more specific instead (ex. view model, entity, etc.).