欢迎各位兄弟 发布技术文章
这里的技术是共享的
class NewsModel extends Model { protected $_scope = array( // 命名范围normal 'normal'=>array( 'where'=>array('status'=>1), ), // 命名范围latest 'latest'=>array( 'order'=>'create_time DESC', 'limit'=>10, ), ); }复制代码'命名范围标识'=>array( '属性1'=>'值1', '属性2'=>'值2', ... )复制代码| where | 查询条件 |
| field | 查询字段 |
| order | 结果排序 |
| table | 查询表名 |
| limit | 结果限制 |
| page | 结果分页 |
| having | having查询 |
| group | group查询 |
| lock | 查询锁定 |
| distinct | 唯一查询 |
| cache | 查询缓存 |
$Model = D('News'); // 这里必须使用D方法 因为命名范围在模型里面定义$Model->scope('normal')->select();$Model->scope('latest')->select();复制代码SELECT * FROM think_news WHERE status=1SELECT * FROM think_news ORDER BY create_time DESC LIMIT 10复制代码$Model->scope('normal')->scope('latest')->select();复制代码$Model->scope('normal,latest')->select();复制代码SELECT * FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 10复制代码$Model->scope('normal,new')->select();复制代码SELECT * FROM think_news WHERE status=1复制代码 protected $_scope = array( // 默认的命名范围 'default'=>array( 'where'=>array('status'=>1), 'limit'=>10, ), );复制代码$Model->scope()->select();复制代码$Model->scope('default')->select();复制代码$Model->scope('normal',array('limit'=>5))->select();复制代码SELECT * FROM think_news WHERE status=1 LIMIT 5复制代码$Model->scope('normal,latest',array('limit'=>5))->select();复制代码SELECT * FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 5复制代码$Model->scope(array('field'=>'id,title','limit'=>5,'where'=>'status=1','order'=>'create_time DESC'))->select();复制代码SELECT id,title FROM think_news WHERE status=1 ORDER BY create_time DESC LIMIT 5复制代码protected $_scope = array( 'normal'=>array( 'where'=>array('status'=>1), 'field'=>'id,title', 'limit'=>10, ), );复制代码$Model->scope('normal')->limit(8)->order('id desc')->select();复制代码SELECT id,title FROM think_news WHERE status=1 ORDER BY id desc LIMIT 8复制代码$Model->limit(8)->scope('normal')->order('id desc')->select();复制代码SELECT id,title FROM think_news WHERE status=1 ORDER BY id desc LIMIT 10复制代码$Model->scope('normal',array('limit'=>5))->select();复制代码$Model->normal(array('limit'=>5))->select();复制代码