企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 模型定义 为了更好的理解,我们首先在数据库创建一个`think_user`表如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">EXISTS</span> <span class="hljs-string">`think_user`</span>( <span class="hljs-string">`id`</span> <span class="hljs-number">int</span>(<span class="hljs-number">8</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-number">NULL</span> AUTO_INCREMENT, <span class="hljs-string">`nickname`</span> <span class="hljs-number">varchar</span>(<span class="hljs-number">50</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-number">NULL</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'昵称'</span>, <span class="hljs-string">`email`</span> <span class="hljs-number">varchar</span>(<span class="hljs-number">255</span>) <span class="hljs-number">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-number">NULL</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'邮箱'</span>, <span class="hljs-string">`birthday`</span> <span class="hljs-number">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">UNSIGNED</span> <span class="hljs-keyword">NOT</span> <span class="hljs-number">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'生日'</span>, <span class="hljs-string">`status`</span> tinyint(<span class="hljs-number">2</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-number">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'状态'</span>, <span class="hljs-string">`create_time`</span> <span class="hljs-number">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">UNSIGNED</span> <span class="hljs-keyword">NOT</span> <span class="hljs-number">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'注册时间'</span>, <span class="hljs-string">`update_time`</span> <span class="hljs-number">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">UNSIGNED</span> <span class="hljs-keyword">NOT</span> <span class="hljs-number">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">'0'</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">'更新时间'</span>, PRIMARY <span class="hljs-keyword">KEY</span> (<span class="hljs-string">`id`</span>) ) <span class="hljs-keyword">ENGINE</span>=MyISAM <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">CHARSET</span>=utf8 ;</span> ``` ``` 数据库配置文件定义如下: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">return</span> [ <span class="hljs-comment">// 数据库类型</span><span class="hljs-string">'type'</span> => <span class="hljs-string">'mysql'</span>, <span class="hljs-comment">// 服务器地址</span><span class="hljs-string">'hostname'</span> => <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-comment">// 数据库名</span><span class="hljs-string">'database'</span> => <span class="hljs-string">'demo'</span>, <span class="hljs-comment">// 数据库用户名</span><span class="hljs-string">'username'</span> => <span class="hljs-string">'root'</span>, <span class="hljs-comment">// 数据库密码</span><span class="hljs-string">'password'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// 数据库连接端口</span><span class="hljs-string">'hostport'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// 数据库连接参数</span><span class="hljs-string">'params'</span> => [], <span class="hljs-comment">// 数据库编码默认采用utf8</span><span class="hljs-string">'charset'</span> => <span class="hljs-string">'utf8'</span>, <span class="hljs-comment">// 数据库表前缀</span><span class="hljs-string">'prefix'</span> => <span class="hljs-string">'think_'</span>, <span class="hljs-comment">// 数据库调试模式</span><span class="hljs-string">'debug'</span> => <span class="hljs-keyword">true</span>, ]; ``` ``` 并添加路由定义(`application/route.php`)如下: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">return</span> [ <span class="hljs-comment">// 全局变量规则定义</span><span class="hljs-string">'__pattern__'</span> => [ <span class="hljs-string">'id'</span> => <span class="hljs-string">'\d+'</span>, ], <span class="hljs-string">'user/index'</span> => <span class="hljs-string">'index/user/index'</span>, <span class="hljs-string">'user/create'</span> => <span class="hljs-string">'index/user/create'</span>, <span class="hljs-string">'user/add'</span> => <span class="hljs-string">'index/user/add'</span>, <span class="hljs-string">'user/add_list'</span> => <span class="hljs-string">'index/user/addList'</span>, <span class="hljs-string">'user/update/:id'</span> => <span class="hljs-string">'index/user/update'</span>, <span class="hljs-string">'user/delete/:id'</span> => <span class="hljs-string">'index/user/delete'</span>, <span class="hljs-string">'user/:id'</span> => <span class="hljs-string">'index/user/read'</span>, ]; ``` ``` 我们为`think_user`表定义一个`User`模型(位于`application/index/model/User.php`)如下: ``` <pre class="calibre18"> ``` namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ } ``` ``` 大多情况下,我们无需为模型定义任何的属性和方法即可完成基础的操作。 ## 设置数据表 模型会自动对应一个数据表,规范是: > ### 数据库前缀+当前的模型类名(不含命名空间) 因为模型类命名是驼峰法,所以获取实际的数据表的时候会自动转换为小写+下划线命名的数据表名称。 如果你的模型命名不符合这一数据表对应规范,可以给当前模型定义单独的数据表,包括两种方式。 ### 设置完整数据表: ``` <pre class="calibre18"> ``` namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 设置完整的数据表(包含前缀)</span><span class="hljs-keyword">protected</span> $table = <span class="hljs-operator">'think_use</span>r'; } ``` ``` ### 设置不带前缀的数据表名: ``` <pre class="calibre18"> ``` namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 设置数据表(不含前缀)</span><span class="hljs-keyword">protected</span> $name = <span class="hljs-operator">'membe</span>r'; } ``` ``` ## 设置数据库连接 如果当前模型类需要使用不同的数据库连接,可以定义模型的`connection`属性,例如: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 设置单独的数据库连接</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$connection</span> = [ <span class="hljs-comment">// 数据库类型</span><span class="hljs-string">'type'</span> => <span class="hljs-string">'mysql'</span>, <span class="hljs-comment">// 服务器地址</span><span class="hljs-string">'hostname'</span> => <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-comment">// 数据库名</span><span class="hljs-string">'database'</span> => <span class="hljs-string">'test'</span>, <span class="hljs-comment">// 数据库用户名</span><span class="hljs-string">'username'</span> => <span class="hljs-string">'root'</span>, <span class="hljs-comment">// 数据库密码</span><span class="hljs-string">'password'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// 数据库连接端口</span><span class="hljs-string">'hostport'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// 数据库连接参数</span><span class="hljs-string">'params'</span> => [], <span class="hljs-comment">// 数据库编码默认采用utf8</span><span class="hljs-string">'charset'</span> => <span class="hljs-string">'utf8'</span>, <span class="hljs-comment">// 数据库表前缀</span><span class="hljs-string">'prefix'</span> => <span class="hljs-string">'think_'</span>, <span class="hljs-comment">// 数据库调试模式</span><span class="hljs-string">'debug'</span> => <span class="hljs-keyword">true</span>, ]; } ``` ``` ## 设置字段信息 默认情况下,系统会自动获取模型对应数据表的字段信息(用于非数据表字段的写入过滤、参数绑定等),如果你希望减少数据库的开销,也可以手动设置数据表的字段信息包括类型(参数绑定是自动判断的无需设置),例如: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 设置当前模型对应的完整数据表名称</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$table</span> = <span class="hljs-string">'think_user'</span>; <span class="hljs-comment">// 设置数据表主键</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$pk</span> = <span class="hljs-string">'id'</span>; <span class="hljs-comment">// 设置当前数据表的字段信息</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$field</span> = [ <span class="hljs-string">'id'</span> => <span class="hljs-string">'int'</span>, <span class="hljs-string">'birthday'</span> => <span class="hljs-string">'int'</span>, <span class="hljs-string">'status'</span> => <span class="hljs-string">'int'</span>, <span class="hljs-string">'create_time'</span> => <span class="hljs-string">'int'</span>, <span class="hljs-string">'update_time'</span> => <span class="hljs-string">'int'</span>, <span class="hljs-string">'nickname'</span>, <span class="hljs-string">'email'</span>, ]; } ``` ``` > 如果字段类型为字符串类型可以不设置类型。