Server : nginx/1.20.1
System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64
User : www ( 1000)
PHP Version : 7.3.28
Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Directory :  /www/wwwroot/0531yanglao.com/app/common/model/
Upload File :
Current Directory [ Writeable ] Root Directory [ Writeable ]


Current File : /www/wwwroot/0531yanglao.com/app/common/model/BaseModel.php
<?php
namespace app\common\model;

use think\Model;
use think\facade\Request;

class BaseModel extends Model
{

    /*protected $globalScope = ['userID'];

    public function scopeUserId($query)
    {
        if(!admin_is_system(admin_user_id())) {
            $query->where('user_id', admin_user_id());
        }

    }*/

    /*public static function onAfterWrite($data)
    {
        halt($data->toArray());
    }*/

    /**
     * 获取当前调用的模块名称
     * 例如:admin, api, store, task,get_class
     * @return string|bool
     */
    protected static function getCalledModule()
    {
        $class = app()->http->getName();
        if($class == 'admin') {
            $class = 'common';
        }

        return $class;
    }

    public function setCreateTimeAttr($value)
    {
        if($value && strlen($value) > 10) {
            return $value / 1000;
        }

        return $value;
    }

    public static function add($data)
    {
        $model = new static();
        $model->save($data);

        return $model;
        //return self::save($data);
    }
    
    public static function detail($where, $with = [])
    {
        $model = self::with($with);
        
        return is_array($where) ? $model->where($where)->find() : $model->find($where);
    }
    
    /**
     * 更新数据
     * @param unknown $model  更新数据对像
     * @param unknown $data 更新数据
     * @return boolean|unknown
     */
    public static function saveData($model, $data)
    {
        empty($model) && $model = new static();

        return $model->save($data);
    }
    
    /**
     * 删除数据 
     * @param unknown $pk   主键
     */
    public static function del($pk) 
    {
        if(!is_array($pk)) {
            $model = self::detail($pk);
            if(empty($model)) {
                return false;
            }
            return $model::destroy($pk);
        }else {
            return self::where($pk)->delete();
        }
        //return $data->delete();
    }
    
    /**
     * 获取列表
     * @param unknown $filter 条件
     * @param unknown $limit 条数
     * @param array $with   关联
     * @param array $order  排序
     * @return unknown
     */
    public static function getList($filter = [], $limit = 10, $order = ['create_time'=>'desc'], $with = [])
    {
        return self::with($with)
            ->where($filter)
            ->order($order)->paginate([
                'list_rows' =>  $limit,
                'query' =>  \request()->param(),
            ], false);
    }

    /**
     * 获取所有数据
     * @param array $filter
     * @param int $limit
     * @param string[] $order
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAll($filter = [], $limit = 0, $order = ['create_time'=>'desc'], $with = [])
    {
        return self::with($with)->where($filter)->order($order)->limit($limit)->select();
    }

    /**
     * 查询指定列
     * @param $where    条件
     * @param $value    值
     * @param string $key   key
     * @return array
     */
    public static function getColumn($where, $value, $key = '')
    {
        return self::where($where)->column($value, $key);
    }

    public static function count($where = [])
    {
        return self::where($where)->count();
    }

    /**
     * 获取推荐位数据
     * @param $r_id 推荐位ID
     * @param int $limit    取条数
     * @param $table_type   类型
     * @param array $with   关联主表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecommend($r_id, $limit = 6, $table_type, $with = [])
    {
        if(empty($with)) {
            $with = [$table_type];
        }
        $return = [];
        $model = new Recommend();
        $data = $model->withJoin($with)->where("cemetery.status", 1)->where('r_id', $r_id)->where('table_type', $table_type)->order('sort ASC')->select()->toArray();
        foreach ($data as $item) {
            $item[$table_type]['sort'] = $item['sort'];
            $return[] = $item[$table_type];
        }

        return $return;
    }
}