


Overview of the four methods of instantiating models in ThinkPHP, four methods of thinkphp_PHP tutorial
Overview of the four methods of instantiating models in ThinkPHP, four methods of thinkphp
This article describes four methods of instantiating models in ThinkPHP, which has very important applications in ThinkPHP programming. The details are as follows:
1. Create a basic model: instantiate a database operation class that comes with the system
Test.Model.class.php page code is as follows:
class TestModel extends Model{ }
UserAction.class.php page code is as follows:
function test(){ $test=M('test');//表示实例化的是自带的Model类,并且传入test值表示操作的是test表 //等同于$test=new TestModel(); $test=$test->select(); print_r($test);//输出test表中所有数据 }
2. Instantiate a custom model
If the database operation is more complex, you need to add some custom database operation methods to the custom Model class
UserModel.class.php page code is as follows:
class UserModel extends Model{ function pyj(){ echo 'pengyanjie'; //其它的一些数据库操作方法 } }
UserAction.class.php page code is as follows:
function user(){ $user=D('User');//实例化自定义的数据库操作类 //等同于$user=new UserModel(); $user->pyj();//调用User模型中的pyj方法 }
Or, you need to instantiate a table, and at the same time, instantiate a custom database operation class written by yourself, the code is as follows:
function love(){ $love=M('test','UserModel'); //$love=new UserModel('test'); $list=$love->select(); dump($list); $love->pyj(); }
3. Instantiate a user model
UserAction.class.php page code is as follows:
function user(){ $user=new UserModel();//等同于$user=D('User'); $list=$user->select(); dump($list); echo $user->aa(); }
The UserModel.class.php page code is as follows:
The class name user corresponds to the table name user, so when instantiating this model in UserAction, there is no need to pass the additional table name. The code is as follows:
class UserModel extends Model{ function aa(){ echo 'pengyanjie'; } }
The difference between this third instantiation model method and the second one is: in your business logic, usually there will be some public business logic, then you use the second M('table name', 'Model name'); such as M('user','CommonModel') will be more convenient;
The third instantiation model method is suitable for more complex business logic for the operated table, but it does not require the use of common business logic. (Its business logic is unique to the user table and does not need to be used in other models).
4. Instantiate an empty model. It does not know which table you want to use when instantiating the operation.
$user=new Model();//等价与$user=M(); $list=$user->query('select * from think_user'); //使用传统的sql语句的方式,如果这样的话,就必须要加表前缀 dump($list);
Attachment: The difference between $user=new UserModel(); and $user=D('user');:
(1). The D method can automatically detect the model class. If it does not exist, it will throw an exception. At the same time, models that have been instantiated will not be instantiated repeatedly. The default D method can only be applied to models under the current project.
(2) If I say this is a front-end application, but I want to instantiate the model of the back-end project, I can use D.
$user=D('admin','user');//会去自动找admin分组下的user模型类
or:
$user=D('admin.user');
I hope the examples described in this article will be helpful to everyone in ThinkPHP programming.
Add your database connection information to the configuration file you generated separately.
It should be added to the config.php file in the Conf directory with your database connection information.
'URL_MODEL' =>1
Is there a comma missing after 1 in English state?
Remember to delete the runtime folder.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.