M
M
MrDrug2015-09-08 00:53:27
Software design
MrDrug, 2015-09-08 00:53:27

Application architecture. How to implement an entity-model relationship?

Good afternoon! I lived happily until I found out that the architecture should have a layer of entity classes that describe the subject area. All business logic must operate on these objects (and not data model objects, as I used to). More or less like this:

на входе $id заказа 

$orders = new Orders();
$order = new Order();
$order = $orders->get_order_by_id($id); //получаем заказ по id
$order->sum = 100; //меняем сумму заказа на 100
$orders->change_order($order);  //изменяем заказ

Everything is beautiful, clear, the code does not depend on the method of storing data, it does not matter to us how and where get_order_by_id gets the data, but change_order saves it, etc. But this is until the fields "Works on order", "Materials" appeared in the Order class by order", which are arrays of objects of type Work and Material.
class  Order
{
   public $works = array();
   public $materials = array();
   public $sum;
}

Well, actually the question is, let's say I need to take out all the orders. Taking into account the added $works and $materials fields in the
$orders->get_order_by_id($id) method
, there are many calls to the database. In addition to getting the order itself from the database, you need to get the works on the order (fill in the $works array) and one more materials on the order (fill in the $materials array) with one more select. Total 3 calls to the database for one order. And if you need to withdraw hundreds of orders? But work objects themselves can contain such fields, for example, a list of performers allowed to perform this work. So you have to climb into the base again. I can't return the "unfinished" work object, without performers. Another thing is that usually, when displaying orders in a table, a list of performers for work is not needed. So that,
Of course, you can get by with one select, but in it you will have to display only one job and one material (let's just say the first ones in the array). But then the Order object will no longer be what it was intended to be; instead of an array of jobs, it will contain only one job.
Has anyone come across how it all works out? Or to hammer on these essences and the problem disappears by itself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
heartdevil, 2015-09-08
@MrDrug

Hello.
Load related works and materials entities not immediately, but on demand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question