🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 命名范围 scope ``` namespace Home\Model; use Think\Model; class NewsModel extends Model { protected $_scope = array( 'normal'=>array( 'where'=>array('status'=>1), ), 'latest'=>array( 'order'=>'create_time DESC', 'limit'=>10, ), // 默认的命名范围 'default'=>array( 'where'=>array('status'=>1), 'limit'=>10, ), ); } //调用 D("News")->scope('normal')->select(); //混合使用 $Model->scope('normal')->limit(8)->order('id desc')->select(); //支持属性 //where , field , order , limit , page , having , group , lock , distinct , cache\ //支持连贯调用 $Model->scope('normal')->scope('latest')->select(); $Model->scope('normal,latest')->select(); //简便操作,若冲突,则前面覆盖后面 //默认调用 $Model->scope()->select(); ``` ## 视图模型 关联表 ``` 在模块下的Model目录 class ChannelViewModel extends ViewModel{ protected $viewFields=array( 'Channel'=>array('id','channel_name','token','_type'=>'RIGHT'), //right对应下表的 'Member'=>array('name','idcard','_on'=>'Channel.id=Member.channel_id') ); } 在控制器中调用 D("ChannelView")->where(array('id' => 47))->select() 以普通视图一样 ``` ## 数据库操作 ## 设置数据库缓存 在`config.php`中配置 `DATA_CACHE_TIME和DATA_CACHE_TYPE` ``` //不带标识符 M('User')->where('id=5')->cache(true)->find(); //不带标识符,带过期时间 M('User')->where('id=5')->cache(true,60)->find(); /带标识符 M('User')->cache('key',60)->find(); ``` ## 指定调用的模型 ``` D('Common/Dept'); D('Common/Dept','Logic'); //调用逻辑层 目录结构为 Common/Logic/DeptLogic.class.php ```