Traits/Think/Model/Extend 改进 增加统计查询和动态查询

This commit is contained in:
ThinkPHP
2013-04-22 14:40:35 +08:00
parent af39b51bcc
commit db91f81567

View File

@@ -15,6 +15,36 @@ trait Extend {
protected $partition = []; protected $partition = [];
/**
* 利用__call方法实现一些特殊的Model方法
* @access public
* @param string $method 方法名称
* @param array $args 调用参数
* @return mixed
*/
public function __call($method,$args) {
if(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){
// 统计查询的实现
$field = isset($args[0])?$args[0]:'*';
return $this->getField(strtoupper($method).'('.$field.') AS tp_'.$method);
}elseif(strtolower(substr($method,0,5))=='getby') {
// 根据某个字段获取记录
$field = parse_name(substr($method,5));
$where[$field] = $args[0];
return $this->where($where)->find();
}elseif(strtolower(substr($method,0,10))=='getfieldby') {
// 根据某个字段获取记录的某个值
$name = parse_name(substr($method,10));
$where[$name] =$args[0];
return $this->where($where)->getField($args[1]);
}elseif(isset($this->_scope[$method])){// 命名范围的单独调用支持
return $this->scope($method,$args[0]);
}else{
E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
return;
}
}
/** /**
* 设置记录的某个字段值 * 设置记录的某个字段值
* 支持使用数据库字段和方法 * 支持使用数据库字段和方法