T
T
topuserman2020-07-10 17:17:45
PHP
topuserman, 2020-07-10 17:17:45

What is the best way to implement data validation in an entity class?

The module that exports online store orders uploads order and customer data.

The order and the buyer are different entities.
When exporting an order, it may be that the buyer is no longer in the system (for example, deleted),
and such an order does not need to be unloaded.

Each order contains a customer ID.

I implemented the Buyer entity like this:

class Buyer {
   public function __construct(int $buyerId ) { ... }
   public function getFullName(): string { ... }
   //...
}


$orders = new OrderList();
foreach ($orders as $orderItem) {
      // тут перебираю заказы, и нужно понять,
      // если пользователя есть, тогда выполняет скрипт дальше 
     // один из вариантов через доп. метод покупателя проверять существование,
     // второй - отлавливать исключения
}


What is the best way to implement this?

For the Buyer entity , add one more isExist(): bool method , or
throw an exception in the constructor?

What is the best way to do it and why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2020-07-10
@ThunderCat

It is necessary to form a method at the OrderList class level that implements a selection with a join for 2 tables, respectively, if the query is correctly formed, then only orders with existing users will be in the results.

D
dmitriy, 2020-07-10
@dmitriylanets

and you should not have such that the order remained and the buyer was deleted,
this is the same as deleting the order and the order positions remained.
when deleting a customer, you need to delete all his orders

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question