In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is orm in thinkphp". In daily operation, I believe many people have doubts about what is orm in thinkphp. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is orm in thinkphp?" Next, please follow the editor to study!
In thinkphp, ORM refers to "object-relational mapping", which is a storage access layer developed for developers to use the database; the main purpose of ORM is to map objects represented by the object model to the sql-based relational model database structure.
The operating environment of this tutorial: Windows7 system, thinkphp v5.1, Dell G3 computer.
Orm in thinkphp
The full name of ORM is Object Relational Mapping, that is, object-relational mapping
O (Object) objects, which are entities in a project, are more precisely data Model, or persistent classes.
R (Relation) relational data
M (Mapping) mapping, the process of mapping objects to relational data and relational data to objects.
A more intuitive understanding is that ORM is based on the idea of OOP to generate SQL statements to add, delete, modify and check.
ThinkPHP's ORM is a storage access layer developed for developers to use the database.
The main purpose is to map the objects represented by the object model to the relational model database structure based on sql.
When you change the properties of the object itself or call the methods of the object, the corresponding sql statements are executed.
In this way, people who write code can better write business logic, rather than repeatedly write add, delete, change and check sql statements.
Application examples in thinkphp
There are two modules for database operations in the TP framework:
Database
Model
Database Module in tp
Reference a description of a document's characteristics
Split into Connection (connector) / Query (querier) / Builder (SQL generator)
Connection connectors are mainly used to connect to databases, and different drivers can be used to connect different types of databases.
The Query querier is used to run sql statements, process results, and map to datasets.
The Builder generator is used to convert the conditions, sorting, and so on we pass in into sql statements.
In these three steps, we can know that if there is an abstract mapping using the idea of ORM, it can only be the Query querier module, but we can take a closer look at the description of the dataset in the TP document.
It is more about encapsulation to provide processing methods for data, such as:
(the following is a small part of the copy from the document)
ToArray converts the data of the data set into an array merge and merges other data diff comparison arrays, returns the key and value intersect comparison array in the subtractive flip exchange data, returns the intersection keys returns all the key names in the data pop deletes the last element in the data shift deletes the first element in the data unshift inserts an element reduce at the beginning of the data returns the array as a string by using a user-defined function
However, it does not provide reverse mapping of relational operations, such as we manipulate the dataset, automatically update the data in the database.
Therefore, in my understanding, there are not many ORM ideas in the database module, and the key point is to understand and apply the model.
Models in tp
Define model file
Namespace app\ index\ model;use think\ Model;// setting class name needs to follow the conversion rules: serpentine to big hump class User extends Model {/ / set primary key field name protected $contention = 'utimestamp'; / / enable automatic maintenance timestamp field (when to insert and when to update) protected $autoWriteTimestamp = true / / after soft deletion is enabled, delete data is only identified by a field to facilitate query, backup, etc. Use SoftDelete; protected $deleteTime = 'delete_time';} for deletion status
The above code has some more content than the model initialization in the first chapter of the document, which is to highlight that the model can accomplish a lot of functions.
This is also the reason for the emergence of ORM: abstract mapping the execution of sql to objects in object-oriented programming.
We can understand that: a row of data in the table represents an new object in our code. If the object is changed, the corresponding row in the table will be updated automatically.
Use model
The code of the demonstration is relatively simple, but actually it can be very flexible.
For example, queries use non-primary key conditions to query, query multiple rows of records, and so on.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.