thinkphp5.1高级查询
web ThinkPHP5.1 2019-01-11 2764 0
关于本站

“最难不过坚持”

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

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

6449745 2613 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)
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\db\Where;
class Articles extends Model {
	/**
	 * 高级查询-快捷查询方式是一种多字段相同查询条件的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询
	 */
	public function indexFun(){
		// 快捷查询-快捷查询方式是一种多字段相同查询条件的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询
		$rs = Db::table('zht_articles')
			->where('articleTitle|articleContent', 'like', 'thinkphp')
			->where('createTime&updateTime', '>', '2016-1-1')
			->find();
		// 区间查询-区间查询是一种同一字段多个查询条件的简化写法
		// 区间查询的查询条件必须使用数组定义方式,支持所有的查询表达式。
		$rs = Db::table('zht_articles')
			->where('articleTitle', ['like', '%thinkphp%'], ['like', '%kancloud%'],'OR')
			->where('articleId', ['>',0], ['<>',10], 'AND')
			->find();
		// 区间查询其实可以用下面的方式替代,更容易理解,因为查询构造器支持对同一个字段多次调用查询条件
		$rs = Db::table('zht_articles')
			->where('articleTitle', 'like', '%thinkphp%')
			->where('articleTitle', 'like', '%kancloud%')
			->where('articleId', '>',0)
			->where('articleId', 'in',[1,3,5,7,9])
			->find();
		// 批量(字段)查询-可以进行多个条件的批量条件查询定义
		$rs = Db::table('zht_articles')
			->where([['articleTitle', 'like', '%thinkphp%'],['articleId', '>',0]])
			->find();
		
		// 注意,V5.1.7+版本数组方式如果使用exp查询的话,一定要用raw方法。
		$rs = Db::table('zht_articles')
			->where([['articleTitle', 'like', '%thinkphp%'],['articleId', 'exp', Db::raw('>clickNum')]])
			->find();
		// 数组查询方式,确保你的查询数组不能被用户提交数据控制,用户提交的表单数据应该是作为查询数组的一个元素传入
		$title = 'thinkphp';
		$id = 10;
		$rs = Db::table('zht_articles')
			->where([['articleTitle', 'like', '%'.$title.'%'],['articleId', '>',$id]])
			->find();
		// 注意,相同的字段的多次查询条件可能会合并,如果希望某一个where方法里面的条件单独处理,可以使用下面的方式,避免被其它条件影响。
		$map = [['articleTitle', 'like', '%thinkphp%'],['articleId', '>',0]];
		$rs = Db::table('zht_articles')
			->where([$map])
			->where('clickNum',1)
			->find();
		// 如果使用下面的多个条件组合-善用多维数组查询,可以很方便的拼装出各种复杂的SQL语句
		$map1 = [['articleTitle', 'like', '%thinkphp%'],['articleContent', 'like', '%thinkphp%']];
		$map2 = [['articleTitle', 'like', '%kancloud%'],['articleContent', 'like', '%kancloud%']];
		$rs = Db::table('zht_articles')
			->whereOr([$map1,$map2])
			->find();
		// 数组对象查询(V5.1.21+)				
		$map = [
			'articleTitle'   => ['like', 'thinkphp%'],
			'articleContent'  => ['like', '%think%'],
			'articleId'     => ['>', 10],
			'clickNum' => 1,
		];
		$where          = new Where;
		$where['articleId']    = ['in', [1, 2, 3, 5, 8]];
		$where['articleTitle'] = ['like', '%php%'];
		$rs = Db::table('zht_articles')
			->where(new Where($map))
			->whereOr($where->enclose()) // enclose方法表示该查询条件两边会加上括号包起来。
			->select();
		// 闭包查询-可见每个闭包条件两边也会自动加上括号,但需要注意,使用闭包查询的时候不能使用cache(true)数据缓存,而应该使用指定key的方式例如cache('key')。
		$name = 'thinkphp';
		$id = 10;
		$rs = Db::table('zht_articles')->where(function ($query) use($name, $id) {
			$query->where('articleTitle', $name)
				->whereOr('articleId', '>', $id);
		})->select();
		// 混合查询
		$rs = Db::table('zht_articles')
			->where('articleTitle', ['like', 'thinkphp%'], ['like', '%thinkphp'])
			->where(function ($query) {
				$query->where('articleId', ['<', 10], ['>', 100], 'or');
			})
			->select();
		// 字符串条件查询
		$rs = Db::table('zht_articles')
			->where('articleTitle LIKE "%thinkphp%" AND articleId>10')			
			->select();
		// 为了安全起见,我们可以对字符串查询条件使用参数绑定
		$rs = Db::table('zht_articles')
			->where('articleTitle LIKE :name AND articleId>:id',['id' => 0, 'name' => 'thinkphp%'])			
			->select();
		// V5.1.8+版本开始,如果你要使用字符串条件查询,推荐使用whereRaw
		$rs = Db::table('zht_articles')
			->whereRaw('articleTitle LIKE :name AND articleId>:id',['id' => 0, 'name' => 'thinkphp%'])			
			->select();
		// 使用Query对象查询(V5.1.5+)-V5.1.5+版本开始,可以通过调用一次where方法传入Query对象来进行查询。
		// Query对象的where方法仅能调用一次,如果query对象里面使用了非查询条件的链式方法,则不会传入当前查询。
		$query = new \think\db\Query;
		$query->where('articleId','>',0)
			->where('articleTitle','like','%thinkphp');		
		$rs = Db::table('zht_articles')
			->where($query)
			->select();
		// 快捷方法
		// 查询update_time大于create_time的用户数据
		$rs = Db::table('zht_articles')
			->whereColumn('updateTime','>','createTime')
			->select();
		// 查询name和nickname相同的用户数据
		$rs = Db::table('zht_articles')
			->whereColumn('articleTitle','=','articleContent')
			->select();
		// 相同字段条件也可以简化为
		$rs = Db::table('zht_articles')
			->whereColumn('articleTitle','articleContent')
			->select();
		// V5.1.11+版本开始,支持数组方式比较多个字段
		$rs = Db::table('zht_articles')
			->whereColumn([['articleTitle','=','articleContent'],['updateTime','>=','createTime']])
			->select();
		// 动态查询-查询构造器还提供了两个动态查询机制,用于简化查询条件,包括getBy和getFieldBy。
		// 其中FieldName表示数据表的实际字段名称的驼峰法表示,假设数据表user中有email和nick_name字段,我们可以这样来查询。
		// getBy和getFieldBy方法只会查询一条记录,可以和其它的链式方法搭配使用
		// 根据邮箱(email)查询用户信息
		$rs = Db::table('zht_articles')
			->whereEmail('thinkphp@qq.com')
			->find();
		// 根据昵称(nick_name)查询用户
		$rs = Db::table('zht_articles')
			->whereNickName('like', '%流年%')
			->find();
		// 根据邮箱查询用户信息
		$rs = Db::table('zht_articles')
    		->getByEmail('thinkphp@qq.com');
		// 根据昵称(nick_name)查询用户信息
		$rs = Db::table('zht_articles')
			->field('articleId,articleTitle')
			->getByNickName('流年');
		// 根据邮箱查询用户的昵称
		$rs = Db::table('zht_articles')
    		->getFieldByEmail('thinkphp@qq.com','nick_name');
		// 根据昵称(nick_name)查询用户邮箱
		$rs = Db::table('zht_articles')
    		->getFieldByNickName('流年', 'email');
		// 条件查询
		// 5.1的查询构造器支持条件查询
		$condition = true;
		$rs = Db::table('zht_articles')->when($condition, function ($query) {
				// 满足条件后执行
				$query->where('clickNum', '>', 10)->limit(10);
			})->select();
		// 并且支持不满足条件的分支查询
		$condition = false;
		$rs = Db::table('zht_articles')->when($condition, function ($query) {
				// 满足条件后执行
				$query->where('clickNum', '>', 10)->limit(10);
			}, function ($query) {
				// 不满足条件执行
				$query->where('clickNum', '>', 1);
			})->select();
		// echo \think\facade\App::version(); //输出thinkphp5.1版本号
		
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	 
}

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

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