php - Why use ORM and the essence of ORM
某草草
某草草 2017-05-16 13:01:38
0
2
832

The explanation found on the Internet is to facilitate switching databases and avoid writing SQL. I don't understand these two very well.
The first one, does switching database mean something like switching mysql to sql server? However, as long as the table structure is the same, the native sql and query constructor do not need to be changed.

Second, avoid writing lengthy SQL, the query constructor can also do this

As for the difference between query constructor and ORM, my understanding is that query constructor is a class that generates SQL, while ORM is a class that maps to a table, and fields are mapped into member variables. I have another question. If there are many tables and fields, wouldn't the ORM class be very verbose? Please correct me on where my understanding is wrong.

$user = DB::table('users')->where('name','Laravel')->first();//laravel query constructor

$posts = Post::where('id','<',3)->orderBy('id','desc')->take(1)->get();//laravel orm 
某草草
某草草

reply all(2)
为情所困

You are right, if there are many tables and many fields, then the ORM class will be very verbose.

The essence of ORM is to map database tables and the relationships between tables to objects and object relationships. It should be noted that although this mapping relationship is bidirectional, there are some problems in the direction of Object->RDB Some limitations, this is because RDB's description of entities is not as rich as Object's description of entities.

Explanation on the two reasons you mentioned:

  1. It is convenient to switch databases. Personally, I think that although ORM provides such a feature, it is not common because it is rare to switch databases in real projects.

  2. Avoid writing SQL. This is an important reason for using ORM, because programmers are lazy and don’t want to learn SQL (even though SQL is easy to learn), and this is also an important reason why ORM was originally produced.

某草草

To give a simple answer, ORM is the abbreviation of object Relational Mapping. Object is an object, relational is a relationship, and mapping is a mapping. Object-relational mapping seems quite abstract, and yes, it is an abstract concept. From the perspective of programming syntax, when specifically operating business objects, there is no need to deal with complex SQL statements, only simple operations on the properties and methods of the objects.

Then answer your two questions


First, there are many differences between mysql and sqlserver. The functions starting with mysql_ that you use in the program will not be recognized by sqlserver. So how do you migrate without changing the code?

Second, ORM contains the following mapping relationships, not just the mapping between tables and classes.

  1. Mapping of classes and tables in the database: Each table in the database corresponds to a class in the programming language

  2. Mapping of objects and records in tables: A table in a relational database may have multiple records, and each record corresponds to an instance of a class

  3. The mapping between the attributes of the class and the fields of the table in the database: the data types of the fields of the table in the database and the types of attributes in the class also correspond one-to-one

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template