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方法进行条件更新,如非必要,尽量不要使用批量更新。
*/
}