ThinkPHP学习笔记(六)实例化模型的四种方法
本文是基于ThinkPHP学习笔记(五)进行修改的,如果有同学使用本代码,请先阅读ThinkPHP学习笔记(五)
创建Action类
<?php class NewObjectAction extends Action{ public function index(){ //1.创建一个基础模型 // //一:实例化一个系统的数据库操作类 // //new Model('User')相当于M('User'),在Model中放置一个需要操作的表 // //1命名: // // thinkphp默认的有表名前缀,如果为think_user;则在Model中可以使用User或者user来获取 // // 如果是think_user_message;则使用Model('UserMessage');大写表示对于的表名前面加上一个_ // $User=new Model('User'); // //调用查询方法 // $list=$User->select(); // dump($list); // //二:跨模型操作;实例化一个表,同时实例化一个自己写的数据库操作类 //多用于将一些公共的业务逻辑抽离出来形成公共的Model // //M('user','CommonModel')相当于new CommonModel('user');model是自动载入的 //// $user=M('user','CommonModel'); // $user=new CommonModel('user'); // $list=$user->select(); // dump($list); // $user->modelTest(); //三:实例化一个用户定义的模型 //多用于不需要更加复杂的业务 //1.手动创建一个模型,针对user表创建一个自定义模型,可以封装thinkphp提供的功能 //$user=new UserModel()相当于D('user'); //D方法如果模型不存在,则抛出异常,只会实例一个,默认,只支持调用当前应用下面的模型 // $user=new UserModel(); // $list=$user->select(); // dump($list); // $user->modelTest(); //四、实例化一个空模型;就是传统sql语句的方式;需要加表前缀 // $user=new Model(); $list=$user->query('select * from tb_user'); dump($list); //分组:D('admin.user'); } } ?>
两个模型:
<?php class CommonModel extends Model{ function modelTest(){ echo '测试的跨模型操作,调用模型中的方法'; } } ?>
<?php class UserModel extends Model{ function modelTest(){ echo '测试的跨模型操作,调用模型中的方法'; } } ?>

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
