SearchModel.php 1.6 KB
<?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);
    }





}