Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值

Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值

Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值

Thinkphp 的文档经常不够完整的表达MYSQL的各种组合,is not null在thinkphp中就不能用“=” 或者简单的eq等来表示。
TP支持MYSQL不为空的array编写方式:
$data = D('tablename');
$map = array();
$map['pic'] = array('exp','is not null');
其中的exp表示MYSQL的表达式查询,支持各种MYSQL语句的添加
该写法同时支持在update中对字段进行自动增值
比如:SQL语句 update table set data=data+1;
用THINKPHP可以表达为
$data['data'] = array('exp','data+1');
$table->where(1)->save($data);

 

例子:

/**
 * [add_msg 发送消息]
 */
public function msg_send($data, $user) {
    if (false === $this->create($data)) return array('state' => 0, 'error' => $this->getError());
    $this->fromuid = $user['uid'];
    if (false === $data['id'] = $this->add()) return array('state' => 0, 'error' => '消息发送失败,请重新操作!');
    if ($data['pid']) $this->where(array('id' => $data['pid']))->setfield('spid', 1);
    if (M('MembersMsgtip')->where(array('uid' => $data['touid'], 'type' => 3))->find()) {
        M('MembersMsgtip')->where(array('uid' => $data['touid'], 'type' => 3))->save(array('update_time' => time(), 'unread' => array('exp', 'unread+1')));
    } else {
        M('MembersMsgtip')->add(array('uid' => $data['touid'], 'type' => 3, 'update_time' => time(), 'unread' => 1));
    }
    if ($user['utype'] == 1) {
        $id = M('Resume')->where(array('uid' => $data['touid']))->order('def desc')->getfield('id');
        $data['to_url'] = $id ? url_rewrite('QS_resumeshow', array('id' => $id)) : '';
        $data['to_name'] = D('Resume')->where(array('uid' => $data['touid'], 'def' => 1))->limit(1)->getfield('fullname');
        if ($data['pid']) {
            $time = $this->where(array('id' => $data['pid']))->getfield('addtime');
            $time = $time + 86400 * 3;
            $count = $this->where(array('pid' => $data['pid'], 'addtime' => array('lt', $time)))->count('id');
            if ($count <= 1) {
                D('TaskLog')->do_task($user, 'reply_consultation');
            }
        }
    } else {
        $company = M('CompanyProfile')->field('id,companyname')->where(array('uid' => $data['touid']))->find();
        $data['to_url'] = url_rewrite('QS_companyshow', array('id' => $company['id']));
        $data['to_name'] = $company['companyname'];
    }
    //写入会员日志
    write_members_log($user, '', '发送站内信消息(消息id:' . $data['pid'] . ',接收人uid:' . $data['touid'] . ',消息内容:' . $data['message'] . ')');
    return array('state' => 1, 'data' => $data);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值