F
F
fsheden2018-11-08 11:58:06
Yii
fsheden, 2018-11-08 11:58:06

I can't understand why Entities are needed in OOP PHP, Yii?

Good day to all, I'm a beginner, I'm studying OOP, I've
learned how to work with repositories, services, helpers ..
and very often in code examples I see such an entity as Entities, but I can't figure out what it is for?
already known data is usually passed there in order to create an object, and then they pull this data with getters or change it with setters ..
but I can do all this without Entity ... help me figure it out, please explain, I feel that this is something very important, without which you can’t program further, but I can’t figure out why))

Answer the question

In order to leave comments, you need to log in

5 answer(s)
T
thyratr0n, 2018-11-08
@fsheden

There are different approaches to understanding the meaning of these things.
Entities are most often used in the context of the DDD approach. There it means that the object can change its state (the shape can change color, the glass can fill, etc.). Entities are opposed by the so-called. ValueObject, which are read-only and cannot change their state.
Both are used in the business logic of the application and are generated either by storage or by services (depending on the chosen approach).
In the context of Yii, the concept of Entity does not apply. because there, the structural unit of the business logic is ActiveRecord instances most often (the framework itself has this), or, sometimes, the heirs of Model.
The main thing is that the entity does not have to be stored as is, i.e. have a clear projection in the database, because the entity can be an instance of the Composite pattern - it all depends on the storage / service that this business will "CRUD"

D
Denis, 2018-11-08
@sidni

Well, if it is very rude and primitive to answer, then in Yii2: Entities replace ActiveRecord Models.
What is the origin of the eternal holivar, why Yii2 is a frame for beginners, because the implementation of ActiveRecord in Yii2 turned out to be very "fat"

D
Denis, 2018-11-15
@prototype_denis

Entity - an entity. First of all, this is what really exists. That is, it is such a data object that has an identifier.
To draw an analogy... Imagine a street of houses that don't have an official address yet. This is a collection of objects. For you, the leftmost from the east is the first, the other from the west thinks that he has the first for you last. That is, the object does not have an identifier. But when they are given addresses, then anyone can determine the third house on the left, because there is an identifier - this is already the essence of the object.
And it does not matter how the entity is called in a certain framework of any language, the concept does not change.
Let's take a plate with a unique name and a non-unique value, in which we will store a certain set of settings for the application. Each line is an entity. But together, when we have all the records in our hands, this is an object that can be represented as an associative array and it will no longer be an entity. It will be an object consisting of entities.
In general, an entity is such an object that has a unique identifier.

E
Eugene Pedya, 2018-11-15
@fpinger

Entities - something that is unique in the system. Uniqueness is fixed in most cases by an identifier.
There may be two Ivan Ivan Ivanovich in the system. If these are two unique people, then each has its own unique identifier.
In contrast, value objects are possible. They are not unique in terms of identification, since we do not need to consider it. We store them as values. Two different entities can have the same value object, for example, a name with the value "Ivan".
In fact, an entity is unique by ID, but can be value equivalent to objects with another entity. But this is rare. In this case, another entity can be associated with the entity. Information about a person can be associated with his authorization information. Thus, two Ivanov Ivan Ivanovich born on the same day can be different users.
Sometimes there may be a requirement in the system that certain entities be distinguished by some value of their object, and not just by the associated entity and identifier.

O
Oleg, 2018-11-08
@402d

Now they throw slippers at me.
But mine is just a dummy class

/**
 * Class Entity_stock
 * @property $id int(11) NOT NULL,
 * @property $distribution_point int(11) NOT NULL COMMENT 'торговая точка',
 * @property $name varchar(255) NOT NULL COMMENT 'наименование',
.....
**/
class Entity_stock{}

Stupidly I do unloading from structure of the table.
What this does is code completion, no need to remember the name of each field
The model returned an object
class Stock_model {
  /**
   * @return Entity_stock
   **/
  public get_by(){
     return обычный вызов класса актив рекорд возвращающий stdClass
  }
}

and the IDE now knows what fields the object has.
I consider the real conversion to an object of a given class to be an unnecessary overhead in my projects.
Again, my opinion about their use does not coincide with the fans. This is not necessary for web building. Extra
overhead. An active setter that checks values ​​for validity is not needed here.
But they don’t go to a foreign monastery with their own charter. They use the doctrine, we write under it. For all getters / seters, we figure out
how to use them correctly.
, so you cannot declare a variable in it, only a public method.
In this case, getters with seters become the only way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question