K
K
kot-airplane2017-07-10 23:11:13
PHP
kot-airplane, 2017-07-10 23:11:13

Which ORM to take for your project?

Haven't worked with ORM before but now I think the time has come. The idea is this: take ORM from any well-known framework, after all, they are available separately on the github, like, install it in your project and use it.
Here questions arose:
1) how do ORMs differ in different frameworks
2) which one is better to take, maybe not a framework at all, I understand that it’s a matter of taste, but here’s what your taste is and why

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-07-10
@kot-airplane

Let's take an ORM from two popular PHP frameworks. The first one will be Eloquent ("Native" for the Laravel framework) and the second one will be Doctrine (One of the available ORMs in the Symfony framework). The cardinal difference between these two "systems" is that the first one is developed on the basis of the Active Record pattern, and the second - using the Data mapper pattern. How are they different? I will give abstract code examples for the first and second patterns:
Active Record :

$user = new User(); // Создаем "сущность" нового пользователя.
$user->login = 'D3lph1'; // Устанавливаем его логин равным 'D3lph1'.
$user->password = '123456'; // Устанавливаем пароль этому пользователю.
$user->save(); // Сохраняем пользователя.

That's it, the new user has been created and is in the database. Now, Data mapper :
$user = new User();
$user->login = 'D3lph1'; // Устанавливаем его логин равным 'D3lph1'.
$user->password = '123456'; // Устанавливаем пароль этому пользователю.

$manager = ... // получаем объект менеджера (Например, из DI контейнера).
$manager->persist($user); // "Скармливаем" новоиспеченного пользователя нашему менеджеру.
// $manager->persis($user1); // Мы можем создать еще одного пользователя и уведомить менеджер об этом.
// $manager->persis($user2); // И еще одного...
$manager->flush(); // После выполнения этого метода данные отправятся в базу данных.

Obviously, the first way is much easier. But not everything is so simple. The fact is that the Active Record pattern violates the Single responsibility SOLID). And therefore, to some extent, it can be considered an anti-pattern. (But this in no way means that it should not be used, for most projects it is "enough" for the eyes). Our user entity does too much. It not only represents data, but also works with it. In large projects, this can make the code more difficult to maintain. Data mapper, on the other hand, separates the representation of data into an entity (user) and work with data (manager, in this example. Also, the repository is responsible for working with data. You will encounter it as soon as you need to get data from the database (Doctrine) ). On smaller projects, you won't notice much of a difference. Unless in the second case the number of classes will increase. So, in Eloquent you create 1 model, and in Doctrine you create an entity and a repository.
All modern ORMs also include so-called query builders. They help move away from a query language like SQL. You will make queries like this:
$result = $qb
      ->select(['id', 'login'])
      ->where('id', '<>', 3)
      ->get();

Actually, query builders help to abstract from a specific DBMS. That is, you wrote the query 1 time, and then the output sql code will depend on which DBMS you use. The generation of this code will be completely transparent to you.
Both ORMs have relationships to work with. You will need to specify how the tables relate to each other, and then you can conveniently access related entities.
Now specifically. Since you are just getting started with ORM, I would recommend starting with Eloquent. It is much simpler than Doctrine, and more productive, besides. As you master Eloquent, feel free to learn how to work with Doctrine. It must be "in the piggy bank" of your skills, as it is the most powerful in the "world" of PHP.
Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question