💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
完成基本的模型定义后,我们就可以进行基础的模型操作了,我们来领略下模型的对象化操作的魅力,主要内容包含: - - [新增数据](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147285#u65B0u589Eu6570u636E) - [批量新增](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147285#u6279u91CFu65B0u589E) - [查询数据](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147285#u67E5u8BE2u6570u636E) - [数据列表](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147285#u6570u636Eu5217u8868) - [更新数据](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147285#u66F4u65B0u6570u636E) - [删除数据](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147285#u5220u9664u6570u636E) ## 新增数据 我们先来看下如何写入模型数据,创建一个`User`控制器并增加`add`操作方法如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>\<span class="hljs-title">User</span> <span class="hljs-title">as</span> <span class="hljs-title">UserModel</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span></span>{ <span class="hljs-comment">// 新增用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-regexp">$user</span>->nickname = <span class="hljs-string">'流年'</span>; <span class="hljs-regexp">$user</span>->email = <span class="hljs-string">'thinkphp@qq.com'</span>; <span class="hljs-regexp">$user</span>->birthday = strtotime(<span class="hljs-string">'1977-03-05'</span>); <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>->save()) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } }</span> ``` ``` > ### 提示: > > - - - - - - > > 在当前文件中给`app\index\model\User`模型定义了一个别名`UserModel`是为了避免和当前的`app\index\controller\User`产生冲突,如果你当前的控制器类不是`User`的话可以不需要定义`UserModel`别名。 有一种方式可以让你省去别名定义,系统支持统一对控制器类添加`Controller`后缀,修改配置参数: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 是否启用控制器类后缀</span><span class="hljs-string">'controller_suffix'</span> => <span class="hljs-keyword">true</span>, ``` ``` 然后,控制器类文件改为`UserController.php`,并且修改控制器类的定义如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>\<span class="hljs-title">User</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span></span>{ <span class="hljs-comment">// 新增用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> User; <span class="hljs-regexp">$user</span>->nickname = <span class="hljs-string">'流年'</span>; <span class="hljs-regexp">$user</span>->email = <span class="hljs-string">'thinkphp@qq.com'</span>; <span class="hljs-regexp">$user</span>->birthday = strtotime(<span class="hljs-string">'1977-03-05'</span>); <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>->save()) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } }</span> ``` ``` 接下来,我们访问 ``` <pre class="calibre18"> ``` http://tp5.com/<span class="hljs-operator">user</span>/<span class="hljs-operator">add</span> ``` ``` 如果看到输出 ``` <pre class="calibre18"> ``` 用户<span class="hljs-operator">[ 流年:1 ]</span>新增成功 ``` ``` 表示用户模型写入成功了。 默认情况下,实例化模型类后执行`save`操作都是执行的数据库`insert`操作,如果你需要实例化执行`save`执行数据库的`update`操作,请确保在save方法之前调用`isUpdate`方法: ``` <pre class="calibre18"> ``` <span class="hljs-regexp">$user</span>->isUpdate()->save(); ``` ``` 如果你觉得上面的方式给`User`对象一个个赋值太麻烦,可以改为下面的方式: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 新增用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span>[<span class="hljs-string">'nickname'</span>] = <span class="hljs-string">'看云'</span>; <span class="hljs-regexp">$user</span>[<span class="hljs-string">'email'</span>] = <span class="hljs-string">'kancloud@qq.com'</span>; <span class="hljs-regexp">$user</span>[<span class="hljs-string">'birthday'</span>] = strtotime(<span class="hljs-string">'2015-04-02'</span>); <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$result</span> = UserModel::create(<span class="hljs-regexp">$user</span>)) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$result</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$result</span>->id . <span class="hljs-string">' ]新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">'新增出错'</span>; } } ``` ``` `create`方法可以传入数组或者标准对象,你可以在外部统一赋值后传入,当然也可以直接传入表单数据(我们后面会有专门的描述)。 我们刷新刚才的访问地址后,页面输出结果为: ``` <pre class="calibre18"> ``` 用户<span class="hljs-operator">[ 看云:2 ]</span>新增成功 ``` ``` ## 批量新增 也可以直接进行数据的批量新增,给控制器添加如下`addList`操作方法: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 批量新增用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addList</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-regexp">$list</span> = [ [<span class="hljs-string">'nickname'</span> => <span class="hljs-string">'张三'</span>, <span class="hljs-string">'email'</span> => <span class="hljs-string">'zhanghsan@qq.com'</span>, <span class="hljs-string">'birthday'</span> => strtotime(<span class="hljs-string">'1988-01-15'</span>)], [<span class="hljs-string">'nickname'</span> => <span class="hljs-string">'李四'</span>, <span class="hljs-string">'email'</span> => <span class="hljs-string">'lisi@qq.com'</span>, <span class="hljs-string">'birthday'</span> => strtotime(<span class="hljs-string">'1990-09-19'</span>)], ]; <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>->saveAll(<span class="hljs-regexp">$list</span>)) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户批量新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } ``` ``` 访问URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/add_list</span> ``` ``` 最后的输出结果为: ``` <pre class="calibre18"> ``` 用户批量新增成功 ``` ``` ## 查询数据 接下来添加`User`模型的查询功能,给`User`控制器增加如下`read`操作方法: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 读取用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span>=<span class="hljs-string">''</span>)</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>); <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; } ``` ``` 模型的`get`方法用于获取数据表的数据并返回当前的模型对象实例,通常只需要传入主键作为参数,如果没有传入任何值的话,则表示获取第一条数据。 访问如下URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/1</span> ``` ``` 输出结果是: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span> ``` ``` 访问如下URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/2</span> ``` ``` 输出结果是: ``` <pre class="calibre18"> ``` 看云 kancloud@qq.com <span class="hljs-number">2016</span>/<span class="hljs-number">04</span>/<span class="hljs-number">02</span> ``` ``` 模型的`get`方法和`Db`类的`find`方法返回结果的区别在于,`Db`类默认返回的只是数组(注意这里说的默认,其实仍然可以设置为对象),而模型的`get`方法查询返回的一定是当前的模型对象实例。 但是系统为模型实现了`ArrayAccess`接口,因此仍然可以通过数组的方式访问对象实例,把控制器的`read`操作方法改成如下: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 读取用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>); <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>[<span class="hljs-string">'nickname'</span>] . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>[<span class="hljs-string">'email'</span>] . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>[<span class="hljs-string">'birthday'</span>]) . <span class="hljs-string">'<br/>'</span>; } ``` ``` 再次访问URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/1</span> ``` ``` 不但没有报错,而且最终的输出结果和之前是一样的: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span> ``` ``` 如果我想通过用户的`email`来查询模型数据的话,应该如何操作呢? 下面是一个查询的例子: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 根据email读取用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::getByEmail(<span class="hljs-string">'thinkphp@qq.com'</span>); <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; } ``` ``` 输出的结果是: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span> ``` ``` 如果不是根据主键查询的话,可以传入数组作为查询条件,例如: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 根据nickname读取用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::get([<span class="hljs-string">'nickname'</span>=><span class="hljs-string">'流年'</span>]); <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; } ``` ``` 更复杂的查询则可以使用查询构建器来完成,例如: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 根据nickname读取用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::where(<span class="hljs-string">'nickname'</span>, <span class="hljs-string">'流年'</span>)->find(); <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; } ``` ``` ## 数据列表 如果要查询多个数据,可以使用模型的`all`方法,我们在控制器中添加index操作方法用于获取用户列表: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 获取用户数据列表</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$list</span> = UserModel::all(); <span class="hljs-keyword">foreach</span> (<span class="hljs-regexp">$list</span> <span class="hljs-keyword">as</span> <span class="hljs-regexp">$user</span>) { <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'----------------------------------<br/>'</span>; } } ``` ``` 然后访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/index</span> ``` ``` 就可以看到输出结果为: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span> ------------------------------------- 看云 kancloud@qq.com <span class="hljs-number">2015</span>/<span class="hljs-number">04</span>/<span class="hljs-number">02</span> ------------------------------------- 张三 zhanghsan@qq.com <span class="hljs-number">1988</span>/<span class="hljs-number">01</span>/<span class="hljs-number">15</span> ------------------------------------- 李四 lisi@qq.com <span class="hljs-number">1990</span>/<span class="hljs-number">09</span>/<span class="hljs-number">19</span> ------------------------------------- ``` ``` 如果不是使用主键查询,可以直接传入数组条件查询,例如: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 获取用户数据列表</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$list</span> = UserModel::all([<span class="hljs-string">'status'</span>=><span class="hljs-number">1</span>]); <span class="hljs-keyword">foreach</span> (<span class="hljs-regexp">$list</span> <span class="hljs-keyword">as</span> <span class="hljs-regexp">$user</span>) { <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'----------------------------------<br/>'</span>; } } ``` ``` 我们也可以使用数据库的查询构建器完成更多的条件查询,例如: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 获取用户数据列表</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$list</span> = UserModel::where(<span class="hljs-string">'id'</span>,<span class="hljs-string">'<'</span>,<span class="hljs-number">3</span>)->select(); <span class="hljs-keyword">foreach</span> (<span class="hljs-regexp">$list</span> <span class="hljs-keyword">as</span> <span class="hljs-regexp">$user</span>) { <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> date(<span class="hljs-string">'Y/m/d'</span>, <span class="hljs-regexp">$user</span>->birthday) . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">'----------------------------------<br/>'</span>; } } ``` ``` 刷新页面访问输出的结果是: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span> ------------------------------------- 看云 kancloud@qq.com <span class="hljs-number">2015</span>/<span class="hljs-number">04</span>/<span class="hljs-number">02</span> ------------------------------------- ``` ``` ## 更新数据 我们可以对查询出来的数据进行更新操作,下面添加一个`update`操作方法: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 更新用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">update</span><span class="hljs-number">(<span class="hljs-regexp">$id</span>)</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>); <span class="hljs-regexp">$user</span>->nickname = <span class="hljs-string">'刘晨'</span>; <span class="hljs-regexp">$user</span>->email = <span class="hljs-string">'liu21st@gmail.com'</span>; <span class="hljs-keyword">if</span> (<span class="hljs-keyword">false</span> !== <span class="hljs-regexp">$user</span>->save()) { <span class="hljs-keyword">return</span> <span class="hljs-string">'更新用户成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } ``` ``` 访问下面的URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/update/1</span> ``` ``` 会输出 ``` <pre class="calibre18"> ``` 更新用户成功 ``` ``` 然后我们再次访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/1</span> ``` ``` 会看到输出结果变成: ``` <pre class="calibre18"> ``` 刘晨 liu21st@gmail.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span> ``` ``` 说明我们的更新操作已经生效了。 默认情况下,查询模型数据后返回的模型示例执行`save`操作都是执行的数据库`update`操作,如果你需要实例化执行`save`执行数据库的`insert`操作,请确保在save方法之前调用`isUpdate`方法: ``` <pre class="calibre18"> ``` <span class="hljs-regexp">$user</span>->isUpdate(<span class="hljs-keyword">false</span>)->save(); ``` ``` `ActiveRecord`模式的更新数据方式需要首先读取对应的数据,如果需要更高效的方法可以把update方法改成: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 更新用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">update</span><span class="hljs-number">(<span class="hljs-regexp">$id</span>)</span></span>{ <span class="hljs-regexp">$user</span>[<span class="hljs-string">'id'</span>] = (int) <span class="hljs-regexp">$id</span>; <span class="hljs-regexp">$user</span>[<span class="hljs-string">'nickname'</span>] = <span class="hljs-string">'刘晨'</span>; <span class="hljs-regexp">$user</span>[<span class="hljs-string">'email'</span>] = <span class="hljs-string">'liu21st@gmail.com'</span>; <span class="hljs-regexp">$result</span> = UserModel::update(<span class="hljs-regexp">$user</span>); <span class="hljs-keyword">if</span> (<span class="hljs-keyword">false</span> !== <span class="hljs-regexp">$result</span>) { <span class="hljs-keyword">return</span> <span class="hljs-string">'更新用户成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } ``` ``` ## 删除数据 我们给User控制器添加delete方法用于删除用户。 ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 删除用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">delete</span><span class="hljs-number">(<span class="hljs-regexp">$id</span>)</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>); <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>) { <span class="hljs-regexp">$user</span>->delete(); <span class="hljs-keyword">return</span> <span class="hljs-string">'删除用户成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">'删除的用户不存在'</span>; } } ``` ``` 然后访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/delete/1</span> ``` ``` 输出结果为: ``` <pre class="calibre18"> ``` 删除用户成功 ``` ``` 如果刷新页面后输出结果为: ``` <pre class="calibre18"> ``` 删除的用户不存在 ``` ``` 同样我们也可以直接使用`destroy`方法删除模型数据,例如把上面的`delete`方法改成如下: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 删除用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">delete</span><span class="hljs-number">(<span class="hljs-regexp">$id</span>)</span></span>{ <span class="hljs-regexp">$result</span> = UserModel::destroy(<span class="hljs-regexp">$id</span>); <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$result</span>) { <span class="hljs-keyword">return</span> <span class="hljs-string">'删除用户成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">'删除的用户不存在'</span>; } } ``` ``` 目前为止,你已经掌握了最基本的模型操作,后面会引申一些高级的用法。