Home > Backend Development > PHP Tutorial > laravel query builder DB or ORM

laravel query builder DB or ORM

WBOY
Release: 2016-08-04 09:21:11
Original
1562 people have browsed it

What is the difference between the two and what scenarios should each be used in?

Reply content:

What is the difference between the two and what scenarios should each be used in?

All our operations are done through ORM, because the operation is simple, intuitive and easy to maintain. The performance is lower but not too fatal. If there is real concurrency, it needs to be optimized. Using DB may not solve the problem. You still need to understand the meaning of each method in the ORM, otherwise you may accidentally end up with a lot more SQL. For example, add a with when fetching the list. Don't just rely on relationships to get other contents of the joint table.

I have never used laravel, but I guess my understanding should be the same. DB directly writes SQL to operate the database, while ORM uses objects to operate the database.

You can use SQL or ORM to operate the database. SQL is the query language of relational databases, and relational databases are the mainstream. Understanding and using SQL is a must. However, program development currently prefers OOP thinking because it has more humane expressive power, while SQL is not as expressive as OOP. A language with better expressiveness is more conducive to humans organizing code logic. A language with more expressiveness will qualitatively optimize the overall development efficiency, the code will be more robust, and there will be fewer bugs.

Debugging large sections of SQL will not be easier than debugging with ORM, and ORM can generally switch between different databases. These are the advantages of ORM. A robust system should use ORM, which can effectively avoid low-level errors caused by mistakes. But the ORM code is not unified. You are familiar with its ORM in laravel and cannot be used directly in other frameworks.

If you have not been in the industry for a long time and are a novice, then use SQL as much as possible, because SQL is inevitably used, and a deep understanding of SQL is a must. Using it can be regarded as practice. If you feel you are already experienced, use an ORM, which can reduce the error rate.

It is recommended to use the DB facade to directly operate the database because ORM performance is low.

For data query, ORM is no worse than DB. For example, with uses the most basic SQL split statement optimization. The loss of ORM is only at the code level, which is no longer a problem.

ORM is suitable for queries of general to medium complexity, and is also suitable for various model operations. For example, if you have a relational target, you can directly use targets()->delete(), etc. to perform relational data operations.
Soft deletion in ORM, automatic update of time fields, field protection, and field type conversion will all benefit you in some standardized and systematic projects.

In addition, DB scenarios: some more complex query statements, transaction operations, etc. need to be completed by DB.

DB is mainly a query builder (SQLBuilder). It will help you convert the input parameters into SQL statements for querying in the database. It is essentially the same as manually writing SQL statements yourself.
ORM is an Object Relational Mapper tool that maps data in the database into objects and collection objects. You do not need to touch the underlying data and can directly call the mapped objects for development.

DB is suitable for projects with high performance requirements or simple business logic, and ORM is suitable for projects with complex business logic.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template