Thinkphp5-thinkPHP database M () used more? -SegmentFault

Source: Internet
Author: User
After learning thinkPHPC and V's last database operations, I felt like I was using the PDO provided by PHP. Now thinkPHP also has a set of database operation classes. I looked at the tutorial and it was complicated to study again .. All the value passing formats have been recorded. I have learned classMy... and learned the operation database of thinkPHP C and V,
The PDO that comes with php I learned earlier will also be used,
Now thinkPHP also comes with a set of database operation classes.
I looked at the tutorial and it was complicated to study again ..
I learned the class MySQLi, and later I learned how to use pdo and pdo. Now I have another M... I want to learn more.

I will ask if thinkPHP's big M method is used much in your project?
Is it more convenient than pdo?

I still want to learn ..

Reply content:

After learning the operation database of thinkPHP C and V,
The PDO that comes with php I learned earlier will also be used,
Now thinkPHP also comes with a set of database operation classes.
I looked at the tutorial and it was complicated to study again ..
I learned the class MySQLi, and later I learned how to use pdo and pdo. Now I have another M... I want to learn more.

I will ask if thinkPHP's big M method is used much in your project?
Is it more convenient than pdo?

I still want to learn ..

  • 27 minutes ago
  • Comment
  • Reply

Please log on first and then comment

Sort by default

Five answers

The answer is helpful to people and has reference value.0The answer is not helpful. It is a wrong answer.

After reading the document, you will feel very convenient to use. What is the significance of the framework ?! Isn't that about letting you write less code! In other words, the tp 3.2.3 database driver is rewritten with pdo (fully pdo-based)

Now that you want to use the Thinkphp framework, you must first use the M or D method!

  • If you want to instantiate the model class file, use D

  • If you do not need to load model classes, use M
    Generally, this is simple.CURDUse M. Use it based on actual conditions. Hope to help you

Thinkphp has two model calling methods: M () and D (), for example, operating the user table:

M('user')->add(array('name' => 'zhangsan'));D('**U**ser')->add(array('name' => 'lisi'));

Note that the first letter of User in D ('user') is capitalized. The difference between the two is that D () can be used by you to write extension model operations, while M () is the built-in method of thinkPHP, that is, D () can implement function expansion, the M () function cannot be expanded, and the performance of D () is weaker than that of M.

You must understand the pdo and mysqli methods when learning PHP, but in a specific project, use the thinkPHP encapsulated method to perform operations.

I use D more

Databases are used in almost every project.ThinkPHPIn this framework, a table is encapsulated into an object. In php, Object Instantiation requires a new class, and the code is similar

// Similar to MysqlTableClass $ obj_Table = new MysqlTableClass ('db _ name', 'table _ name'); obj_test = $ obj_Table-> getOne ();

WhileThinkPHPOfMFunction (here I have to correct your statement. It is called a method in the class,This M is located in a public function, so it can only be called a function.), Encapsulates the new operation above, and returns an object directly.

Skill development

Do not useMMethod, because when you need to add a custom model later,MThe method must be changed one by oneD, It is very troublesome, And if you directly useDTo avoid such problems.

To sum up

Simply use the D method, which is essentially an encapsulation ~

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.