年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1712|回复: 0

模型中定义命名范围

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2015-12-24 16:11:02 | 显示全部楼层 |阅读模式
    UserModel.class.php
    [PHP] 纯文本查看 复制代码
    <?php
    namespace Models\Model;
    use Think\Model;
    
    // 对应数据表:user
    class UserModel extends Model{
    	
    	protected $_scope = array(
    		'latest' => array(
    			'order' => 'date',
    			'limit' => 3,
    		),
    		'VIP'	=> array(
    			'where'	=> array(
    				'vip' => 1,
    			)
    		)
    	);
    	
    	// 获取最近注册的10个用户
    	public function getLatestUser(){
    		$userModel = $this->limit(3)->order('date desc')->select();
    		return dump($userModel);
    	}
    	// 获取最近注册的10个VIP用户
    	public function getLatestVIPUser(){
    		//$userModel = $this->where('vip = 1')->limit(2)->order('date')->select();
    		// 使用命名范围
    		$userModel = $this->scope('latest')->scope('VIP')->select();
    		return dump($userModel);
    	}
    	// 获取10个VIP用户,2015年注册的,并且按积分从高到低排序
    	public function getLatestSomeUser(){
    		$condition = array(
    			'vip' => 1
    		);
    		$userModel = $this->where($condition)->where('date > '.strtotime(2015-01-01))->limit(2)->order('score')->select();
    		// 同样可以使用命名范围减化语句
    		return dump($userModel);
    	}
    }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-16 22:57 , Processed in 0.053173 second(s), 22 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表