关于本站
“最难不过坚持”
本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作
有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339
2712
39
分类目录
最新评论
- https://jueru.net/
-
评 留言
- :weixiao:
-
评 留言
- :shuijiao: :weiqu: :zhenbang: :leng:
-
评 留言
- :yiwen: :yiwen: :yiwen: :yiwen:
-
评 EasySass: could not generate CSS file. See Output panel for details.
- 这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
-
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\facade\Request;
class Articles extends Model {
protected $pk = 'articleId';
/**
* 更新-查找并更新-在取出数据后,更改字段内容后使用save方法更新数据。这种方式是最佳的更新方式。
*/
public function edit1(){
$articles = Articles::get(1);
$articles->articleTitle = 'thinkphp33';
$articles->articleContent = 'thinkphp@qq.com';
$rs = $articles->save();
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-查找并更新-对于复杂的查询条件,也可以使用查询构造器来查询数据并更新
*/
public function edit2(){
$articles = Articles::where('catId',5)
->where('articleTitle','thinkphp33')
->find();
$articles->articleTitle = 'thinkphp44';
$articles->articleContent = 'thinkphp@qq.com';
$rs = $articles->save();
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-save方法更新数据,只会更新变化的数据,对于没有变化的数据是不会进行重新更新的。如果你需要强制更新数据,可以使用下面的方法:
*/
public function edit3(){
$articles = Articles::get(1);
$articles->articleTitle = 'thinkphp44';
$articles->articleContent = 'thinkphp@qq.com';
$rs = $articles->force()->save();
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-如果要执行SQL函数更新,那么在V5.1.8+版本可以使用下面的方法
*/
public function edit4(){
$articles = Articles::get(1);
$articles->articleTitle = 'thinkphp44';
$articles->articleContent = 'thinkphp@qq.com';
$articles->clickNum = Db::raw('clickNum+1');
$rs = $articles->save();
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-如果只是字段的增加/减少,也可以直接用inc/dec方式
*/
public function edit5(){
$articles = Articles::get(1);
$articles->articleTitle = 'thinkphp44';
$articles->articleContent = 'thinkphp@qq.com';
$articles->clickNum = ['inc', 1];
$rs = $articles->save();
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-直接更新数据-也可以直接带更新条件来更新数据
*/
public function edit6(){
$articles = new Articles;
// save方法第二个参数为更新条件
$rs = $articles->save([
'articleTitle'=>'thinkphp22',
'articleContent'=>'thinkphp@qq.com'
],['articleId'=>1]);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-如果需要过滤非数据表字段的数据
*/
public function edit7(){
$articles = new Articles;
// 过滤post数组中的非数据表字段数据
$rs = $articles->allowField(true)->save($_GET,['articleId' => 1]);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:
*/
public function edit8(){
$articles = new Articles;
// post数组中只有name和email字段会写入
$rs = $articles->allowField(['articleTitle','articleContent'])->save($_GET,['articleId' => 1]);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-最佳建议是在传入模型数据之前就进行过滤
*/
public function edit9(){
$articles = new Articles;
// post数组中只有name和email字段会写入
$data = Request::only(['articleTitle','articleContent']);
$rs = $articles->save($data,['articleId' => 1]);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-批量更新数据-可以使用saveAll方法批量更新数据,只需要在批量更新的数据中包含主键即可
*/
public function edit10(){
$articles = new Articles;
$list = [
['articleId'=>133, 'articleTitle'=>'thinkphp', 'articleContent'=>'thinkphp@qq.com'],
['articleId'=>132, 'articleTitle'=>'onethink', 'articleContent'=>'onethink@qq.com']
];
$rs = $articles->saveAll($list);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-静态方法-模型支持调用数据库的方法直接更新数据
*/
public function edit11(){
$rs = Articles::where('articleId', 1)
->update(['articleTitle' => 'thinkphp55']);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-静态方法-或者使用模型的静态update方法更新:
*/
public function edit12(){
$rs = Articles::update(['articleId'=>1,'articleTitle' => 'thinkphp5566']);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-如果你的数据操作比较复杂,可以用isUpdate方法显式的指定当前调用save方法是新增操作还是更新操作。-显式更新数据
*/
public function edit13(){
$articles = new Articles;
// save方法第二个参数为更新条件
$rs = $articles->isUpdate(true)
->save([
'articleId'=>1,
'articleTitle'=>'thinkphp22',
'articleContent'=>'thinkphp@qq.com'
]);
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 更新-如果你的数据操作比较复杂,可以用isUpdate方法显式的指定当前调用save方法是新增操作还是更新操作。-显式新增数据
*/
public function edit(){
//$articles = Articles::get(1);// 提示错误SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'
$articles = new Articles;
$articles->articleTitle = 'hahha';
$rs = $articles->isUpdate(false)
->save();
echo Db::getlastsql();
dump($rs);
exit;
return $rs;
}
/**
* 最佳实践-更新的最佳实践原则是:如果需要使用模型事件,那么就先查询后更新,如果不需要使用事件,直接使用静态的Update方法进行条件更新,如非必要,尽量不要使用批量更新。
*/
}
赏
相关推荐
Non-static method think\Config::get() should not be called statically
原来是这样use think\Config;
改成这样use think\facade\Config;
下面是官方手册的解释
配置获取
要使用Config类,首先需要在你的类文件中引入
use think\facade\Config;
或者(因为系统做了类库别名,其实就是调用think\facade\Config)
u...
thinkPHP5 order多条件排序
总结如下:
//单一条件排序
$user = $this->where(['parentId'=>0)->field('userId,userName,userSort,isShow')->order('userSort', 'asc')->select();
//多个条件排序,可以多加一个order...
评论加载中...
前一篇: thinkphp5.1模型新增
后一篇: thinkphp5.1模型删除