// +---------------------------------------------------------------------- namespace Admin\Model; use Think\Model; /** * 文档基础模型 */ class ServerModel extends Model{ /* 自动验证规则 */ protected $_validate = array( array('game_id', '/^[1-9]\d*$/', '请选择游戏', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH), array('server_name', 'require', '区服名称不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH), array('server_name', '1,30', '区服名称不能超过30个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH), array("server_name,game_id,id", 'checkServerName', '同游戏下区服名称已存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH), array('start_time', 'require', '开始时间不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH), ); /* 自动完成规则 */ protected $_auto = array( array('create_time', 'getCreateTime', self::MODEL_INSERT,'callback'), array('server_num', 0, self::MODEL_INSERT), array('start_time', 'strtotime', self::MODEL_BOTH, 'function'), array('parent_id', 0,self::MODEL_INSERT,'string'), ); /** * 构造函数 * @param string $name 模型名称 * @param string $tablePrefix 表前缀 * @param mixed $connection 数据库连接信息 */ public function __construct($name = '', $tablePrefix = '', $connection = '') { /* 设置默认的表前缀 */ $this->tablePrefix ='tab_'; /* 执行构造方法 */ parent::__construct($name, $tablePrefix, $connection); } /** * 创建时间不写则取当前时间 * @return int 时间戳 * @author huajie */ protected function getCreateTime(){ $create_time = I('post.create_time'); return $create_time?strtotime($create_time):NOW_TIME; } /** * 生成不重复的name标识 * @author huajie */ private function generateName(){ $str = 'abcdefghijklmnopqrstuvwxyz0123456789'; //源字符串 $min = 10; $max = 39; $name = false; while (true){ $length = rand($min, $max); //生成的标识长度 $name = substr(str_shuffle(substr($str,0,26)), 0, 1); //第一个字母 $name .= substr(str_shuffle($str), 0, $length); //检查是否已存在 $res = $this->getFieldByName($name, 'id'); if(!$res){ break; } } return $name; } /** *判断同游戏下区服名称是否存在 */ public function checkServerName($args=null){ $map['game_id'] = $args['game_id']; $map['server_name'] = $args['server_name']; if(empty($args['id'])){ $data = $this->field('id')->where($map)->find(); return empty($data); }else{ $data = $this->field('id')->where($map)->find(); return empty($data['id'])?true:$data['id'] == $args['id'] ? true:false; } } }