Learning Essentials:
1. Create a database
2. Instantiation of the model
3. Field definitions
In this lesson, we'll focus on the thinkphp Model Operations section. The model is the M in MVC, which can be understood as manipulating the database part operations.
I. Creating a Database
Before using the model operation, we first create a database: thinkphp. Creates a user table: User.
Add some data.
The thinkphp contains an abstract database access layer, which encapsulates different database operations. We only need to operate with a common Db class, without having to write different code and the underlying implementation for different databases. The DB class automatically calls the appropriate database driver for processing.
Currently supported databases include MySQL (including MySQL and mysqli), SQL Server, Pgsql, Sqlite, Oracle, Ibase, Mongo, PDO, and more.
Db_type settings |
Supported Database types |
MySQL or mysqli |
Mysql |
Pgsql |
Pgsql |
Sqlite |
Sqlite |
Mssql |
Sql server |
Oracle |
Oracle |
IBase |
IBase |
Mongo |
Mongo |
Pdo |
All databases supported by PDO |
Global configuration definition
' Db_type ' = ' mysql ',//database type
' db_host ' = ' localhost ',//server address
' Db_name ' = ' thinkphp ',//database name
' Db_user ' = ' root ',//user name
' Db_pwd ' = ' 123456 ',//password
' Db_port ' =>3306,//Port
' Db_prefix ' = ' think_ ',//database table prefix
PS: Database Global configuration information in addition to PDO, you can use the above settings.
PDO-specific definition
' Db_type ' = ' pdo ',//database type
' Db_user ' = ' root ',//user name
' Db_pwd ' = ' 123456 ',//password
' Db_prefix ' = ' think_ ',//database table prefix
' Db_dsn ' = ' Mysql:host=localhost;dbname=thinkphp;charset=utf8 ',
Two. Instantiation of the model
After connecting the database, we need to manipulate the data from the database, then we need to instantiate the model class. In thinkphp, the Model base class processing is provided, or the M () method can be used.
Instantiate model class, pass a data table name
$user = new Model (' user ');
Show variable structure
Var_dump ($user);
The Model base class can pass three parameters:
Model ([' model name '],[' data table prefix '],[' database connection information ']); Instantiate model class, change table prefix
$user = new Model (' User ', ' tp_ ');
Instantiate model class, define database link information
$user =new Model (' user ', ' think_ ', ' mysql://root:[email protected]/thinkphp ');
Print out all the data
Var_dump ($user->select ());
Using the Model base class also requires importing namespaces, and using the M () method is not required.
Instantiate model class
$user = M (' user ');
In addition to using the model base class and the M () method, there is a definition of a corresponding data table, such as: Usermodel.
This model class does not have to be defined, only if there is a separate business logic or attribute.
User Model Class
namespace Home\model;
Use Think\model;
Class Usermodel extends Model {}
Once the Usermodel model class is created, the controller can declare it directly.
User Model Class
$user = new Usermodel ();
Var_dump ($user->select ());
Why does the Usermodel model class have direct access without specifying any tables? Because this model class basically operates the data table directly, it is corresponding to the data table name on the naming specification.
While it is convenient to use model classes and data tables, sometimes we need to change the table names, prefixes, additional database names, and so on, requiring some field-defined operations. To make it easier to understand the changes in the data table, we use the page
Face Trace tool, you can always query SQL changes. page trace, debugging Accessibility tools
' Show_page_trace ' =>true,
Redefining table prefixes
Class Usermodel extends Model
{
protected $tablePrefix = ' abc_ ';
}
Redefine table name
Class Usermodel extends Model
{
protected $tableName = ' abc ';
}
Redefine the full prefixed table name
Class Usermodel extends Model
{
protected $trueTableName = ' tp_abc ';
}
Attach database name
Class Usermodel extends Model
{
protected $dbName = ' TP ';
}
If you only use database basic operations such as curd, we recommend using the M () method based on the Model base class.
Using the M () method will result in higher performance because you do not need to load specific model classes (such as the Usermodel Class).
Of course, if it is necessary to use a specific model class, thinkphp also provides the D () method to instantiate the model class directly, and also eliminates the introduction of namespaces and other operations.
Instantiate the Usermodel class
$user = D (' user ');
PS: Using the D () method is more intelligent than using the model class directly, if the model class is not found in \home\model\usermodel, then go to the public module to find \common\model\usermodel. If it is not found, the base class Model () class is instantiated directly, which is equivalent to using the M () method.
The D () method can directly invoke the model class of the current module, so what if it is called across modules? For example, the admin backend module, you can use the directory declaration.
Cross-module instantiation
$user = D (' Admin/user ');
Sometimes, you might want to manipulate the database using native SQL statements. You can either instantiate the empty model base class or the empty M () method.
Empty M () method
$user = M (); or new Model (); Empty base class
Var_dump ($user->query ("select * from Think_user WHERE user= ' crayon Small New ')");
Three. Field definitions
Each model class operates on each of the corresponding data tables, and in most cases the system automatically obtains field information for the current data table. When the model class is first instantiated, the system automatically caches the fields and caches them permanently, unless the runtime cache is deleted or the settings are not cached.
If in debug mode, the field cache file is not generated and is retrieved from the data table each time. The purpose of generating the cache is obvious, in order to respond quickly. thinkphp default is to turn on the field cache because the field structure is not changed in the actual run.
The field cache file is saved in the runtime/data/_fields/directory, and when you are in the development phase, the fields and tables change frequently, so close the cache. The way to turn off caching is:
Close field Cache
' Db_fields_cache ' =>false//debug mode turned on, auto-off
PS: If the state of the cache is turned on and new fields are added, you may not be able to refresh the newly created fields, you must delete the/data/_fields folder and retrieve the fields again.
View field Structure
Var_dump ($user->getdbfields ());
You can also replace field caching by manually defining data table fields, which improves performance and avoids IO overhead.
Manually define a data table field, _PK represents a primary key
Class Usermodel extends Model
{
Protected $fields = array (' ID ', ' user ', ' _pk ' = ' id ');
}
Type defines the types of each field and can be field-verified forever
Class Usermodel extends Model
{
Protected $fields = array (' ID ', ' user ', ' _pk ' = ' id ', ' type ' =>array (' id ' = ' smallint ', ' user ' = ' varchar '));
}
Preliminary study on thinkphp--model