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);
}
}
|