Answer the question
In order to leave comments, you need to log in
Which design pattern is best for loading related entities?
Not tied to any framework.
For example, there is an order entity, it contains product identifiers, a customer identifier, etc. How to implement all this beautifully and correctly using modern approaches?
Here is an example code:
class OrderEntity
{
protected $id = 0;
protected $member_id = 0;
// массив идентифкаторов
protected $items_ids = [];
}
class ItemEntity
{
protected $id = 0;
protected $name = '';
}
class MemberEntity
{
protected $id = 0;
protected $name = '';
}
class OrderService
{
public function getOrder($id): OrderEntity
{
return $this->mapper->getOrder($id);
}
}
$service = new OrderService();
$order = $service->getOrder(1);
Answer the question
In order to leave comments, you need to log in
1. Somehow your entities live a separate life from each other. In fact, now you have transferred the structure of your database to objects, and this is not correct. The order entity must aggregate the entities of the order and customer items, and not just contain their identifiers. For example:
class OrderEntity
{
/**
* @var integer Идентификатор заказа
*/
protected $id;
/**
* @var MemberEntity Объект покупателя
*/
protected $member;
/**
* @var ItemEntity Массив позиций заказа
protected $items = [];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question