ThinkPHP中的exp查询
2022-11-24 11:10:29
192
{{single.collect_count}}
select * fromvendor where vendor_id = vendor_f_id

在thinkphp中为了代码的简洁以及通用性,不考虑使用原生的方式进行sql的查询,而是采用查询map的方式进行查询

$condition[ 'vendor_f_id' ] = 'vendor_id';

但是,thinkphp在处理上述条件的时候,将其转化为了以下代码:

select * from vendor where vendor_f_id = 'vendor_id'

也就是说,vendor_id被当成了一个字符串进行处理了

解决上述问题的办法是:

$condition[ 'vendor_f_id' ] = [ 'exp' , ' = vendor_id ' ];

exp表达式
exp不是一个运算符,而是一个综合表达式以支持更复杂的条件设置,exp的操作条件不会被当成字符串,可以使用任何SQL支持的语法,包括使用函数和字段的名称。
exp不仅用于where条件,也可以用于数据更新

$model = M('news');//文章的浏览数 + 1$data['browse'] = array('exp', 'browse + 1');
回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}