In thinkphp, creating a model requires inheriting the think\Model class. Once you inherit this class, you can use the various functions and properties provided to interact with the database. Below, we will introduce the settings of the thinkphp model from the following aspects.
Database connection settings
In thinkphp, we can set the connection information with the database through the database.php file in the application directory. . This file contains all database-related configuration information, such as host name, user name, password, database name, etc. For details, please refer to the following code:
return [ // 默认数据库配置 'database' => [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => 'localhost', // 数据库名 'database' => 'test', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'think_', ], ];
We can modify the contents of the above configuration file according to the actual situation to achieve connection with the database.
Definition of model
In thinkphp, we can operate the database by defining a model. We can think of a model as a mapping of a data table, that is, every attribute in the model corresponds to every field in the table.
We can define the corresponding model through the following code:
namespace app\index\model; use think\Model; class User extends Model { // }
The above code defines a model named User and implements the operation of the corresponding data table.
Setting of model attributes
In thinkphp, we can control how it works by setting model attributes. For example, we can set the table name, primary key, whether to automatically write timestamps, etc. The following are some commonly used attribute setting methods:
1) $table: Set the table name corresponding to the model.
protected $table = 'user';
2) $pk: Set the primary key of the table.
protected $pk = 'id';
3) $autoWriteTimestamp: Set whether to automatically write timestamp.
protected $autoWriteTimestamp = true;
4) $createTime: Set the creation time field name.
protected $createTime = 'create_time';
5) $updateTime: Set the update time field name.
protected $updateTime = 'update_time';
Can be set according to specific business needs.
Model association operations
In thinkphp, we can use the association method in the model attributes to perform association operations on different models. The following are some commonly used model association operations:
1) One-to-one association
public function profile() { return $this->hasOne('Profile', 'user_id'); }
2) One-to-many association
public function comments() { return $this->hasMany('Comment', 'blog_id'); }
3) Many-to-many association
public function roles() { return $this->belongsToMany('Role', 'user_role', 'role_id', 'user_id'); }
The above code is mainly for some simple relationships, and can be further studied and adjusted according to business needs.
The above is the detailed content of How to set up thinkphp model. For more information, please follow other related articles on the PHP Chinese website!