完善Model

This commit is contained in:
thinkphp
2016-04-05 22:14:30 +08:00
parent e5e386e1fd
commit 0265a36941

View File

@@ -125,7 +125,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
/** /**
* 设置数据对象的值 修改器 * 修改器 设置数据对象的值
* @access public * @access public
* @param string $name 名称 * @param string $name 名称
* @param mixed $value 值 * @param mixed $value 值
@@ -148,7 +148,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
/** /**
* 获取数据对象的值 获取器 * 获取器 获取数据对象的值
* @access public * @access public
* @param string $name 名称 * @param string $name 名称
* @return mixed * @return mixed
@@ -156,13 +156,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function __get($name) public function __get($name)
{ {
$value = isset($this->data[$name]) ? $this->data[$name] : null; $value = isset($this->data[$name]) ? $this->data[$name] : null;
// 检测获取器
// 检测属性获取器
$method = 'get' . Loader::parseName($name, 1) . 'Attr'; $method = 'get' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->$method($value, $this->data); return $this->$method($value, $this->data);
} }
if (is_null($value) && method_exists($this, $name)) { if (is_null($value) && method_exists($this, $name)) {
// 执行关联定义方法 关联定义方法始终返回Db对象 // 执行关联定义方法
$db = $this->$name(); $db = $this->$name();
// 判断关联类型执行查询 // 判断关联类型执行查询
switch ($this->relation) { switch ($this->relation) {
@@ -178,6 +180,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case self::BELONGS_TO_MANY: case self::BELONGS_TO_MANY:
$result = $db->select(); $result = $db->select();
break; break;
default:
// 直接返回
$result = $db;
} }
// 避免影响其它操作方法 // 避免影响其它操作方法
$this->relation = null; $this->relation = null;
@@ -342,6 +347,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
'rule' => $rule, 'rule' => $rule,
'msg' => is_array($msg) ? $msg : [], 'msg' => is_array($msg) ? $msg : [],
]; ];
} else {
$this->validate = $rule;
} }
return $this; return $this;
} }