设置数据表
1 模型会自动对应数据表,模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名,并且首字母大写,例如:
模型名 | 约定对应数据表(假设数据库的前缀定义是 think_) |
---|---|
User | think_user |
UserType | think_user_type |
2 如果你想指定数据表甚至数据库连接的话,可以使用:
namespace app\index\model;
class User extends \think\Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'think_user';
// 设置当前模型的数据库连接
protected $connection = [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'thinkphp',
// 数据库用户名
'username' => 'root',
// 数据库密码
'password' => '',
// 数据库编码默认采