thinkphp5.1模型新增
web ThinkPHP5.1 2019-01-14 2169 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6475835 2614 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不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\facade\Request;
class Articles extends Model {
	protected $pk = 'articleId';
	/**
	 * 新增
	 */
	public function add1(){
		$articles           = new Articles;
		$articles->articleTitle     = 'thinkphp';
		$articles->articleContent    = 'thinkphp@qq.com';
		$rs = $articles->save();
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-也可以直接传入数据到save方法批量赋值:
	 */
	public function add2(){
		$articles           = new Articles;
		$rs = $articles->save([
				'articleTitle'  =>  'thinkphp2',
				'articleContent' =>  'thinkphp2@qq.com'
			]);
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-或者直接在实例化的时候传入数据
	 */
	public function add3(){
		$articles = new Articles([
				'articleTitle'  =>  'thinkphp3',
				'articleContent' =>  'thinkphp3@qq.com'
			]);
		$rs = $articles->save();
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-如果需要过滤非数据表字段的数据,可以使用
	 */
	public function add4(){
		$articles = new Articles;
		// 过滤post数组中的非数据表字段数据
		$rs = $articles->allowField(true)->save($_GET);
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用
	 */
	public function add5(){
		$articles = new Articles;
		// 过滤post数组中的非数据表字段数据
		$rs = $articles->allowField(['articleTitle','articleContent'])->save($_GET);
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-最佳的建议是模型数据赋值之前就进行数据过滤
	 */
	public function add6(){
		$articles = new Articles;
		// 过滤post数组中的非数据表字段数据
		$data = Request::only(['articleTitle','articleContent']);
		$rs = $articles->save($data);
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-Replace写入-V5.1.14+版本开始,save方法可以支持replace写入。
	 */
	public function add7(){
		$articles           = new Articles;
		$articles->articleTitle     = 'thinkphp';
		$articles->articleContent    = 'thinkphp@qq.com';
		$articles->articleC    = 'thinkphp@qq.com'; // 不存在的字段
		$rs = $articles->replace()->save(); // 不太明白replace是什么作用,即便删除了,拥有不存在的字段,依然可以正常保存数据
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-如果要获取新增数据的自增ID,其实是获取模型的主键
	 */
	public function add8(){
		$articles           = new Articles;
		$articles->articleTitle     = 'thinkphp';
		$articles->articleContent    = 'thinkphp@qq.com';		
		$rs = $articles->save(); 
		// 获取自增ID
		echo $articles->articleId;
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-添加多条数据
	 */
	public function add9(){
		$articles           = new Articles;
		$list = [
			['articleTitle'=>'thinkphp','articleContent'=>'thinkphp@qq.com'],
			['articleTitle'=>'onethink','articleContent'=>'onethink@qq.com']
		];
		$rs = $articles->saveAll($list);
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-saveAll方法新增数据默认会自动识别数据是需要新增还是更新操作,当数据中存在主键的时候会认为是更新操作
	 */
	public function add10(){
		$articles           = new Articles;
		$list = [
			['articleId'=>125,'articleTitle'=>'thinkphp33','articleContent'=>'thinkphp@qq.com'],
			['articleId'=>126,'articleTitle'=>'onethink33','articleContent'=>'onethink@qq.com']
		];
		$rs = $articles->saveAll($list); // public function saveAll($dataSet, $replace = true)$replace 是否自动识别更新和写入
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-如果你需要带主键数据批量新增,可以使用下面的方式:
	 */
	public function add11(){
		$articles           = new Articles;
		$list = [
			['articleId'=>128,'articleTitle'=>'thinkphp33','articleContent'=>'thinkphp@qq.com'],
			['articleId'=>129,'articleTitle'=>'onethink33','articleContent'=>'onethink@qq.com']
		];
		$rs = $articles->saveAll($list,false); // public function saveAll($dataSet, $replace = true)$replace 是否自动识别更新和写入
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 新增-静态方法-还可以直接静态调用create方法创建并写入:
	 */
	public function add12(){
		$user = Articles::create([
			'articleTitle'  =>  'thinkphp',
			'articleContent' =>  'thinkphp@qq.com'
		]); // 和save方法不同的是,create方法返回的是当前模型的对象实例。
		echo $user->articleTitle;
		echo $user->articleContent;
		echo $user->articleId; // 获取自增ID
		echo Db::getlastsql();
		dump($user);
		exit;
		return $rs;
	}
	/**
	 * 新增-静态方法-create方法的第二个参数可以传入允许写入的字段列表(传入true则表示仅允许写入数据表定义的字段数据)
	 */
	public function add13(){
		// 只允许写入name和email字段的数据
		$user = Articles::create([
			'articleTitle'  =>  'thinkphp',
			'articleContent' =>  'thinkphp@qq.com'
		],['articleTitle']); 
		echo $user->articleTitle;
		echo $user->articleContent;
		echo $user->articleId; // 获取自增ID
		echo Db::getlastsql();
		dump($user);
		exit;
		return $rs;
	}
	/**
	 * 新增-静态方法-V5.1.14+版本开始,支持replace操作,使用下面的方法:
	 */
	public function add(){
		// 只允许写入name和email字段的数据
		$user = Articles::create([
			'articleTitle'  =>  'thinkphp',
			'articleContent' =>  'thinkphp@qq.com'
		],['articleTitle'],true); 
		echo $user->articleTitle;
		echo $user->articleContent;
		echo $user->articleId; // 获取自增ID
		echo Db::getlastsql();
		dump($user);
		exit;
		return $rs;
	}
	/**
	 * 新增-最佳实践-新增数据的最佳实践原则:使用create方法新增数据,使用saveAll批量新增数据。
	 */
}

版权声明:本篇文章来源于网络。 来源链接

相关推荐
Non-static method think\Config::get() should not be called statically
ThinkPHP5.1 | 2018-12-29 10441
原来是这样use think\Config; 改成这样use think\facade\Config; 下面是官方手册的解释 配置获取 要使用Config类,首先需要在你的类文件中引入 use think\facade\Config; 或者(因为系统做了类库别名,其实就是调用think\facade\Config) u...
thinkPHP5 order多条件排序
ThinkPHP5.1 | 2017-10-24 10291
总结如下: //单一条件排序 $user = $this->where(['parentId'=>0)->field('userId,userName,userSort,isShow')->order('userSort', 'asc')->select(); //多个条件排序,可以多加一个order...
评论:0条
评论加载中...
发表评论