SearchModel.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Created by PhpStorm.
* auther: sgj
* Date: 2018/12/11
* Time: 16:33
*/
namespace app\portal\model;
use think\Model;
use traits\model\SoftDelete;
class SearchModel extends Model
{
use SoftDelete;
protected $deleteTime = 'delete_time';
// 开启自动写入时间戳字段
// protected $autoWriteTimestamp = true;
// protected $insert =['add_time'];
protected function setAddTimeAttr()
{
return time();
}
/**
* 获取搜索关键字
* @param $user_id
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getKeyword($user_id,$limit='8'){
$return=$this->where('user_id',$user_id)->limit($limit)->order('id','desc')->select();
return $return;
}
/**
* 关键字添加信息
* @param $user_id
* @param $keyword
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function serarchAdd($user_id,$keyword){
$data['user_id']=$user_id;
$data['name']=$keyword;
/*判断之前是否有相关数据*/
$info=$this->getKeyword($user_id)->toArray();
if (!empty($info)){
foreach ($info as $k=>$v) {
if ($v['name']==$keyword){
$this::destroy($v);
}
}
}
$this->insert($data);
}
}