What is the difference between ORM, AR, QB and DM?

Well, that's why everyone decided (and not in vain) strait SQL from the devil. This leaves us with many methods for placing the “average person” in our code in order to separate our code from the database. Now I'm going to spit out all the information that I have collected, in the hope that someone can tell me and tell me what I built.

ORM (Object Relational Mapping) is a series of tools (tightly or tightly integrated) that map database rows to objects in an application.

In AR (Active-Record), this is an ORM type in which a table or database view is wrapped in a class, so the object instance is bound to one row in the table.

Data Mapping (DM) is an ORM type that is the process of creating data item mappings between two different data models.

All three apply for work as follows:

$user = new User(); $user->name = 'Fred'; $user->save(); 

Usually with the User class something like this:

 class User extends Model { // Specify the database table protected $table = "users"; // Define your fields protected $fields = array( 'id' => array('type' => 'int', 'primary' => true), 'name' => array('type' => 'string', 'required' => true), 'email' => array('type' => 'text', 'required' => true) ); } 

With this setting, you can easily get rows without having to write SQL.

 // users $users = $user->fetch(array('id' => 3)); 

Some AR classes are actually more like this:

 $db->where('id' => 3); $db->join('posts', 'posts.user_id = users.id'); $results = $db->get('users'); 

OK, now this is where he gets hairy. Everyone and his brother seem to have a different idea of ​​what type of code falls where. Although most agree that AR or DM is an ORM type, sometimes the lines that report AR from DM appear to be brushstrokes.

I wrote a class that uses a single object ($ db), in which you call this object, and processes the creation of SQL to save / get the result.

 //Fetch the users $results = $db->select('id, name')->where('id > 4')->get('users'); //Set them active while($user = $results->fetch()) { $user->active = TRUE; $user->save(); } 

So the question is, “what is it?” And why do people disagree with these terms?

+4
source share
3 answers

You could also compose the acronyms ORM, RM DM regardless of ... this is just a state , transferred from one medium to another and wrapped in function / semantics,

Sun Microsystems and Microsoft do this all the time with Java and C #. Let me take something simple and give it a new name! What a great idea.

If you say ORM .. everyone knows what it is, in its many guises. However, your code looks like Linq stuff.

No magic, but a lot of buzzwords and fuss IMHO.

+4
source

Not that straight SQL was the devil - sometimes it was pretty much necessary for him to write raw SQL to get the query to execute the way you want it to. For me, ORM is much more important to exclude manual work than anything else (for example, to avoid SQL at all costs). I just don’t want to configure all my code objects with all manually processed requests for each project. This is just a ridiculous amount of work and thought. Instead, ORM tools provide a convenient and automatic way to create data objects that would otherwise require a lot of manual work. Now I can have automatic separate string objects that I can expand and create custom functions without thinking about it. I can get related rows from different tables without requiring to process this query manually.

It looks like your user class example is from phpDataMapper , and if so, it also has some other built-in subtleties, such as auto table migrations, so you do not need to distribute the SQL file for your table structures, as well as some helper functions and other things. All features designed to save you time are your primary goal.

The most important difference between AR and DM is that the ActiveRecord (AR) line is aware of this own data store and, therefore, has save / update functions for each line object - $ user-> save () - "record", " active. " With DataMapper (DM), on the other hand, each individual row does NOT know this by its own data store by definition. A string is more of an object of dumb value that you can work with in code. Mapper is responsible for translating the changes to this line back to the data store - $ mapper-> save ($ user). What is the most significant difference - you will find that most of the core ORM functions are the same for almost any implementation. This is mainly a question of how it fits together at a basic architectural level.

+5
source

Agreed with @ Aiden Bell. But I think you're right in pointing out the differences. I am using LINQ to SQL, which in your definition is just an Active-Record style for ORM. This works fine for me, as I work on a lot of new applications and start with db to build my classes. From there, I usually follow a domain-driven approach (I know ... this is the first concept of classes) working with my generated classes. For me, this brings me quickly.

Having said that, I also worked with Entity Framework and NHybernate. Entity Framework is an UBER data collector, and NHybernate is also a data mapper, but without such complexity! I personally think Entity Framework has a long way to go. If I ever need more complexity, for example, in an application with a brown field, where db was sufficiently installed in the stone, and I needed my classes to represent my application, and not my database, then I would rather go to what something more NHybernate and data mapping functionality.

I think each of these tools has a place. It’s unfair to say that they are the same as not. An active record is remarkable in that it generates classes for me that directly represent my database. This works when I create a database or a database that represents my ubiquitous application terms and concepts. Oorms matching types work when I have a fixed db that does not make sense or is explicitly displayed on my application, in which case I can encapsulate the complexity or lack of my database to create a rich domain through the display functions of the ORM Tool.

0
source

Source: https://habr.com/ru/post/1286438/


All Articles