ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
controller model/User.php model与 view 是平行的。 ~~~ <?php namespace app\index\model; use think\Model; class User extends Model{ } ~~~ 举例 ~~~ namespace app\index\controller; user app\index\model\User as userModel; class User { public function add(){ $user = new userModel; $user->name='zhangsan'; $user->age = 21; $user->create_time = date('Y-m-d H:i:s'); $user->save(); } } 如果 controller_suffix = 'true'; 就写成: class UserController{ public function add(){ $user['name'] = 'lisi'; $user['age'] = 21; $user['create_time'] = date('Y-m-d H:i:s'); $UserModel::create($user); } } ~~~ 模型定义 ~~~ namespace app\index\model; use think\Model; class User extend Model{ protected $table = 'user'; // 含前缀 protected $name = 'user'; //不含前缀 protected $connection=[ 'type'='mysql', 'hostname'='127.0.0.1', 'database'='test', 'password'='123456', ...... ]; } ~~~