


thinkphp framework model (database query), thinkphp framework_PHP tutorial
Jul 12, 2016 am 08:55 AMthinkphp framework model (database query), thinkphp framework
1. Model definition
The file name must be the table name Model.class.php For example: UserModel.class.php
namespace Home\Model; <span>//</span><span>該模型類(lèi)的命名空間</span> <span>use</span> Think\Model; <span>//</span><span>下面的模型類(lèi)需要繼承Model基類(lèi),Model是系統(tǒng)定義的模型類(lèi),在 Think\Model 這個(gè)命名空間中</span> <span>class</span> UserModel <span>extends</span><span> Model { }</span>
The class name in the file is consistent with the "table name" in the file name ( This item is required. If it is inconsistent, the model class will not be found in the controller ).
The corresponding relationship between class name and table name is (Note: There is an underscore in front of the data table corresponding to each upper case in the model name, as shown in the second line below )
模型名 | 約定對(duì)應(yīng)數(shù)據(jù)表(如果數(shù)據(jù)庫(kù)的前綴是think_) |
UserModel | think_user |
UserTypeModel | think_user_type |
2. 模型實(shí)例化
2.1 直接實(shí)例化
2.1.1 不帶參數(shù)實(shí)例化 $user=new \Home\Model\UserModel();
2.1.2 帶參數(shù)實(shí)例化 $user=new \Home\Model\UserModel(['模型名'],['數(shù)據(jù)表前綴'],['數(shù)據(jù)庫(kù)連接信息']);
上述兩種方法的區(qū)別在于,第一種在實(shí)例化的時(shí)候回去讀取默認(rèn)配置(例如在模型中的配置或者是模塊中的數(shù)據(jù)庫(kù)配置),而第二種方法是直接讀取傳入?yún)?shù)的數(shù)據(jù)庫(kù)配置
模型名是對(duì)應(yīng)的數(shù)據(jù)庫(kù)中的去掉前綴的表名,如果不指定這會(huì)讀取模型類(lèi)的名稱(chēng)但是是去掉Model后的名稱(chēng) 例如:
UsereModel.class.php 文件中的 UsereModel 類(lèi) ,如果在實(shí)例化的時(shí)候傳入模型名"User"會(huì)按傳入查找數(shù)據(jù)表 前綴+“user”,如果不傳則會(huì)提取 UsereModel類(lèi)的 “Usere” 去查找數(shù)據(jù)表。這樣可以解決模型名與數(shù)據(jù)表明不一致的問(wèn)題。
2.2 D方法實(shí)例化
D方法的參數(shù)是模型的名稱(chēng),并且和模型類(lèi)的大寫(xiě)定義是一致的。
<?<span>php </span><span>//</span><span>實(shí)例化模型</span> <span>$user</span>=D("User"<span>); </span><span>//</span><span>相當(dāng)于 $user=new \Home\Model\UserModel(); </span>
? ? ?跨模塊實(shí)例化
? ? ?D('Admin/User') 其中 “Admin” 是模塊名稱(chēng),“User”是模型名稱(chēng) ?
? ? 實(shí)例化 Extend擴(kuò)展名空間下的Info模型
? ? ? D('Extend://Editor/Info')
? ? 2.3 M方法實(shí)例化
? ? ? ? M('User') 與D方法實(shí)例化的區(qū)別是這個(gè)方法不需要該模型存在,并且如果模型存在則其中的方法也會(huì)別忽略,該方法主要是用來(lái)對(duì)數(shù)據(jù)庫(kù)的操作。
? ? ? ? ? 當(dāng)D方法沒(méi)有找到模型時(shí)會(huì)默認(rèn)的調(diào)用 M方法。
? ? 2.4 ?實(shí)例化空模型
? ? ? ? ?$Model=new Model() 或者 $Model=M();
? ? ? ? ? 這種空實(shí)例可以用老進(jìn)行原生的sql查詢(注:其他的模型也可以進(jìn)行原生的sql查詢,并且查詢的表不必跟模型對(duì)應(yīng))
3. 模型中的字段
? ?$fields=array('','') 表示數(shù)據(jù)表中的字段
? ?$pk 表示數(shù)據(jù)表的主鍵
? ?$connection=array('db_type' => 'mysql','db_user' => 'root','db_pwd' => '1234','db_host' => 'localhost','db_port' => '3306','db_name' => 'thinkphp','db_charset' => 'utf8','db_params' => array(), // 非必須); ?表示數(shù)據(jù)庫(kù)連接
? ?數(shù)據(jù)庫(kù)類(lèi)型://用戶名:密碼@數(shù)據(jù)庫(kù)地址:數(shù)據(jù)庫(kù)端口/數(shù)據(jù)庫(kù)名#字符集
? ? $connection = 'mysql://root:1234@localhost:3306/thinkphp#utf8';
?

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.
