Thinkphp中的Think\Model类提供了数据库的基本CURD(Create、Update、Read及Delete),通过该类可以很便捷的进行操作。
Model类及扩展类主要的方法有:
这些方法的可能的返回值有false/true/一位数组/二维数组/numeric(数字)/null/类本身的实例,现在就总结如下:
所以链式操作都返回Model类的实例,即可以使用该返回值继续调用实例方法。
<span class="pln">$User<span class="pun">=<span class="pln">M<span class="pun">(<span class="str">"User"<span class="pun">);</span></span></span></span></span></span>
<span class="pln">$U1<span class="pun">=<span class="pln">$User<span class="pun">-><span class="pln">$where<span class="pun">(<span class="str">"id = 1"<span class="pun">);<span class="com">//$U1可以继续调用其他方法。</span></span></span></span></span></span></span></span></span>
<span class="pln">$result<span class="pun">=<span class="pln">$U1<span class="pun">-><span class="kwd">select<span class="pun">();</span></span></span></span></span></span>
<span class="com">//-------一般情况下没有必要这样,下面一句代码即可实现</span>
<span class="pln">$result<span class="pun">=<span class="pln">M<span class="pun">(<span class="str">"User"<span class="pun">)-><span class="pln">$where<span class="pun">(<span class="str">"id = 1"<span class="pun">)-><span class="kwd">select<span class="pun">();</span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">$User <span class="pun">=<span class="pln"> M<span class="pun">(<span class="str">"User"<span class="pun">);</span></span></span></span></span></span>
<span class="kwd">if<span class="pun">(<span class="pln">$User<span class="pun">-><span class="pln">create<span class="pun">()){</span></span></span></span></span></span>
<span class="com">//进行更多操作</span>
<span class="pun">}</span>
delete 返回值:成功则返回true,查询错误则返回false
出处:http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html