model() public static method

实例化(分层)模型
public static model ( string $name = '', string $layer = 'model', boolean $appendSuffix = false, string $common = 'common' ) : Object
$name string Model名称
$layer string 业务层名称
$appendSuffix boolean 是否添加类名后缀
$common string 公共模块名
return Object
Example #1
0
 /**
  * 利用TP核心的单图片上传方法
  */
 public function picture()
 {
     $Storage = Loader::model('Storage');
     $options = ['ext' => ['jpg', 'gif', 'png', 'jpeg']];
     $info = $Storage->upload('upload', $options);
     if (false !== $info) {
         return $this->success('上传成功', '', $info);
     } else {
         return $this->error($Storage->getError());
     }
 }
Example #2
0
 /**
  * [edit description]
  * @param  integer $id [description]
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Category');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Category')->update($data)) {
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $this->assign('info', Db::name('Category')->find($id));
         $this->assign('upcate_list', Loader::model('Category')->treeSelect('', $id));
         return $this->fetch('edit');
     }
 }
Example #3
0
File: Menu.php Project: cjango/cwms
 /**
  * 编辑
  * @param  [type] $id 主键
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Menu');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Menu')->update($data)) {
             session('system_menu_list', null);
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $this->assign('up_menus', self::_treeShow($id));
         $this->assign('info', Db::name('Menu')->where('id', $id)->find());
         return $this->fetch();
     }
 }
Example #4
0
 /**
  * 关联数据验证
  * @access protected
  * @param mixed $data  数据对象
  * @param string $name 关联名称
  * @return mixed
  */
 protected function crRelation(&$data, $name = '')
 {
     if (empty($data) && !empty($this->data)) {
         $data = $this->data;
     } elseif (!is_array($data)) {
         // 数据无效返回
         return false;
     }
     if (!empty($this->_link)) {
         $fields = $this->getFields();
         // 遍历关联定义
         foreach ($this->_link as $key => $val) {
             // 操作制定关联类型
             $mappingName = !empty($val['mapping_name']) ? $val['mapping_name'] : $key;
             // 映射名称
             if (empty($name) || true === $name || $mappingName == $name || is_array($name) && in_array($mappingName, $name)) {
                 // 操作制定的关联
                 $mappingType = !empty($val['mapping_type']) ? $val['mapping_type'] : $val;
                 // 关联类型
                 $mappingClass = !empty($val['class_name']) ? $val['class_name'] : $key;
                 // 关联类名
                 $mappingKey = !empty($val['mapping_key']) ? $val['mapping_key'] : $this->getPk();
                 // 关联键名
                 if (strtoupper($mappingClass) == strtoupper($this->name) || !isset($data[$mappingName])) {
                     continue;
                     // 自引用关联或提交关联数据跳过
                 }
                 // 获取关联model对象
                 $model = \think\Loader::model($mappingClass);
                 $_data = $data[$mappingName];
                 unset($data[$key]);
                 if ($_data = $model->token(false)->create($_data)) {
                     $data[$mappingName] = $_data;
                     $fields[] = $mappingName;
                 } else {
                     $error = $model->getError();
                     if ($this->patchValidate) {
                         $this->error[$mappingName] = $error;
                     } else {
                         $this->error = $error;
                         return false;
                     }
                 }
             }
         }
         // 过滤非法字段数据
         $diff = array_diff(array_keys($data), $fields);
         foreach ($diff as $key) {
             unset($data[$key]);
         }
     }
 }
Example #5
0
/**
 * 实例化Model
 * @param string $name Model名称
 * @param string $layer 业务层名称
 * @return object
 */
function D($name = '', $layer = MODEL_LAYER)
{
    return \think\Loader::model($name, $layer);
}
Example #6
0
 /**
  * 实例化Model
  * @param string    $name Model名称
  * @param string    $layer 业务层名称
  * @param bool      $appendSuffix 是否添加类名后缀
  * @return \think\Model
  */
 function model($name = '', $layer = 'model', $appendSuffix = false)
 {
     return Loader::model($name, $layer, $appendSuffix);
 }
Example #7
0
File: View.php Project: cnzin/think
 /**
  * 检查是否定义了所有字段
  * @access protected
  * @param string $name  模型名称
  * @param array $fields 字段数组
  * @return array
  */
 private function _checkFields($name, $fields)
 {
     if (false !== ($pos = array_search('*', $fields))) {
         // 定义所有字段
         $fields = array_merge($fields, \think\Loader::model($name)->getFields());
         unset($fields[$pos]);
     }
     return $fields;
 }
Example #8
0
/**
 * 实例化Model
 * @param string $name Model名称
 * @param string $layer 业务层名称
 * @return \think\Model
 */
function model($name = '', $layer = MODEL_LAYER)
{
    return Loader::model($name, $layer);
}
Example #9
0
 /**
  * 解析规则路由
  * @access private
  * @param string    $rule 路由规则
  * @param string    $route 路由地址
  * @param string    $pathinfo URL地址
  * @param array     $option 路由参数
  * @param array     $matches 匹配的变量
  * @return array
  */
 private static function parseRule($rule, $route, $pathinfo, $option = [], $matches = [])
 {
     $request = Request::instance();
     // 解析路由规则
     if ($rule) {
         $rule = explode('/', $rule);
         // 获取URL地址中的参数
         $paths = explode('|', $pathinfo);
         foreach ($rule as $item) {
             $fun = '';
             if (0 === strpos($item, '[:')) {
                 $item = substr($item, 1, -1);
             }
             if (0 === strpos($item, ':')) {
                 $var = substr($item, 1);
                 $matches[$var] = array_shift($paths);
             } else {
                 // 过滤URL中的静态变量
                 array_shift($paths);
             }
         }
     } else {
         $paths = explode('|', $pathinfo);
     }
     // 获取路由地址规则
     if (is_string($route) && isset($option['prefix'])) {
         // 路由地址前缀
         $route = $option['prefix'] . $route;
     }
     // 替换路由地址中的变量
     if (is_string($route) && !empty($matches)) {
         foreach ($matches as $key => $val) {
             if (false !== strpos($route, ':' . $key)) {
                 $route = str_replace(':' . $key, $val, $route);
                 unset($matches[$key]);
             }
         }
     }
     // 绑定模型数据
     if (isset($option['bind_model'])) {
         $bind = [];
         foreach ($option['bind_model'] as $key => $val) {
             if ($val instanceof \Closure) {
                 $result = call_user_func_array($val, [$matches]);
             } else {
                 if (is_array($val)) {
                     $fields = explode('&', $val[1]);
                     $model = $val[0];
                     $exception = isset($val[2]) ? $val[2] : true;
                 } else {
                     $fields = ['id'];
                     $model = $val;
                     $exception = true;
                 }
                 $where = [];
                 $match = true;
                 foreach ($fields as $field) {
                     if (!isset($matches[$field])) {
                         $match = false;
                         break;
                     } else {
                         $where[$field] = $matches[$field];
                     }
                 }
                 if ($match) {
                     $query = strpos($model, '\\') ? $model::where($where) : Loader::model($model)->where($where);
                     $result = $query->failException($exception)->find();
                 }
             }
             if (!empty($result)) {
                 $bind[$key] = $result;
             }
         }
         $request->bind($bind);
     }
     // 解析额外参数
     self::parseUrlParams(empty($paths) ? '' : implode('|', $paths), $matches);
     // 记录匹配的路由信息
     $request->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]);
     // 检测路由after行为
     if (!empty($option['after_behavior'])) {
         if ($option['after_behavior'] instanceof \Closure) {
             $result = call_user_func_array($option['after_behavior'], []);
         } else {
             foreach ((array) $option['after_behavior'] as $behavior) {
                 $result = Hook::exec($behavior, '');
                 if (!is_null($result)) {
                     break;
                 }
             }
         }
         // 路由规则重定向
         if ($result instanceof Response) {
             return ['type' => 'response', 'response' => $result];
         } elseif (is_array($result)) {
             return $result;
         }
     }
     if ($route instanceof \Closure) {
         // 执行闭包
         $result = ['type' => 'function', 'function' => $route];
     } elseif (0 === strpos($route, '/') || strpos($route, '://')) {
         // 路由到重定向地址
         $result = ['type' => 'redirect', 'url' => $route, 'status' => isset($option['status']) ? $option['status'] : 301];
     } elseif (false !== strpos($route, '\\')) {
         // 路由到方法
         $route = str_replace('/', '@', $route);
         $method = strpos($route, '@') ? explode('@', $route) : $route;
         $result = ['type' => 'method', 'method' => $method];
     } elseif (0 === strpos($route, '@')) {
         // 路由到控制器
         $result = ['type' => 'controller', 'controller' => substr($route, 1)];
     } else {
         // 路由到模块/控制器/操作
         $result = self::parseModule($route);
     }
     // 开启请求缓存
     if ($request->isGet() && !empty($option['cache'])) {
         $cache = $option['cache'];
         if (is_array($cache)) {
             list($key, $expire) = $cache;
         } else {
             $key = $pathinfo;
             $expire = $cache;
         }
         $request->cache($key, $expire);
     }
     return $result;
 }
Example #10
0
 /**
  * 编辑配置
  * @param  integer $id 配置主键
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Config');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Config')->update($data)) {
             Cache::clear();
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $this->assign('info', Db::name('Config')->find($id));
         return $this->fetch();
     }
 }
Example #11
0
 /**
  * 编辑关键字
  * @return [type]
  */
 public function keyword_edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('WechatKeyword');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('WechatKeyword')->update($data)) {
             return $this->success('', url('system/weixin/keyword'));
         } else {
             return $this->error();
         }
     } else {
         $info = Db::name('WechatKeyword')->find($id);
         $this->assign('contentType', self::keyword_type($info['type'], $info['content']));
         $this->assign('info', $info);
         return $this->fetch();
     }
 }
Example #12
0
 /**
  * 编辑
  * @param  [type] $id [description]
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Member');
         if (!$validate->scene('edit')->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Member')->update($data)) {
             logs('用户编辑成功', $data);
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $level = Db::name('member')->find($id);
         $map = ['id' => ['neq', 1], 'status' => 1, 'level' => $level['level'] - 1];
         $list = Db::name('member')->where($map)->field('id,nickname')->select();
         $this->assign('list', $list);
         $info = Db::name('Member')->find($id);
         $this->assign('info', $info);
         return $this->fetch();
     }
 }