Laravel query constructor DB or ORM

Source: Internet
Author: User
What are the differences between the two? what are the differences between the two in each scenario?

Reply content:

What are the differences between the two and what scenarios should they be used in?

All our operations are based on the orm, because the operation is simple, intuitive, and well maintained, and the performance is lower, but it is not fatal yet, the database cannot be used to solve the problem if concurrency needs to be optimized. You still need to understand the meaning of each orm method. Otherwise, you may encounter many more SQL statements accidentally, for example, adding "with" to the list does not just rely on the relationship to obtain other contents of the table.

Laravel has never been used, but it should be understood that DB directly writes SQL statements to operate databases, and ORM uses objects to operate databases.

Both SQL and ORM can be used to operate databases. SQL is the query language of relational databases, while Relational databases are the mainstream. it is necessary to understand and use SQL. However, at present, program development tends to be more oriented to OOP because it has a more user-friendly expressiveness, while SQL expressions are less powerful than OOP. A language with better expression ability is more conducive to organizing the code logic by humans. a language with better expression ability is optimized throughout the development efficiency, and the code is more stable, there may be fewer bugs.

Debugging of large SQL segments is not easier than ORM debugging, and ORM can usually switch between different databases. these are the advantages of ORM. Stable systems should use ORM, which can effectively avoid low-level errors caused by mistakes. However, the ORM Code is not uniform. you are familiar with its ORM in laravel and cannot directly use it in other frameworks.

If you are a newbie, use SQL as much as possible, because SQL is inevitable. it is necessary to have a deep understanding of SQL and can be used as an exercise. If you feel that you are already experienced, use ORM to reduce the loss rate.

We recommend that you use the database facade to directly operate the database, because the ORM performance is low.

In data query, ORM is not inferior to DB, such as with, which is the most basic SQL statement splitting optimization. The loss of ORM is only at the code level, which is no longer a problem.

ORM is applicable to queries of medium complexity and various model operations. for example, if you have a link targets, you can directly use targets ()-> delete () to perform relational data operations.
In ORM, soft deletion, automatic update of time fields, field protection, and field type conversion will benefit you in some standard and system engineering.

In addition, DB scenarios: some complex query statements, transaction operations, and so on all need to be completed by DB.

DB is mainly a query constructor (SQLBuilder). it will help you convert input parameters into SQL statements for query in the database. it is essentially the same as manually writing SQL statements.
ORM is an Object Relational Mapper tool that maps data in the database to objects and collection objects. you do not need to access the underlying data, you 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 complicated business logic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.