改进Model类和Query类 Model类的name属性改为static定义

This commit is contained in:
thinkphp
2016-05-04 16:24:51 +08:00
parent df39f1706f
commit 6b26b1ee25
3 changed files with 18 additions and 16 deletions

View File

@@ -24,6 +24,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
private static $links = []; private static $links = [];
// 数据库配置 // 数据库配置
protected static $connection = []; protected static $connection = [];
// 当前模型名称
protected static $name;
// 数据表名称 // 数据表名称
protected static $table; protected static $table;
// 回调事件 // 回调事件
@@ -33,8 +35,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $pk; protected $pk;
// 错误信息 // 错误信息
protected $error; protected $error;
// 当前模型名称
protected $name;
// 字段验证规则 // 字段验证规则
protected $validate; protected $validate;
@@ -86,7 +86,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} else { } else {
$this->data = $data; $this->data = $data;
} }
$this->name = basename(str_replace('\\', '/', get_class($this))); if (empty(static::$name)) {
static::$name = basename(str_replace('\\', '/', get_class($this)));
}
$this->initialize(); $this->initialize();
$this->relation = new Relation($this); $this->relation = new Relation($this);
@@ -392,7 +394,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 设置字段验证 * 设置字段验证
* @access public * @access public
* @param array|bool $rule 验证规则 true表示自动读取验证器类 * @param array|string|bool $rule 验证规则 true表示自动读取验证器类
* @param array $msg 提示信息 * @param array $msg 提示信息
* @return $this * @return $this
*/ */
@@ -404,7 +406,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
'msg' => $msg, 'msg' => $msg,
]; ];
} else { } else {
$this->validate = true === $rule ? $this->name : $rule; $this->validate = true === $rule ? static::$name : $rule;
} }
return $this; return $this;
} }
@@ -423,7 +425,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$validate->rule($info['rule']); $validate->rule($info['rule']);
$validate->message($info['msg']); $validate->message($info['msg']);
} else { } else {
$name = is_string($info) ? $info : $this->name; $name = is_string($info) ? $info : static::$name;
if (strpos($name, '.')) { if (strpos($name, '.')) {
list($name, $scene) = explode('.', $name); list($name, $scene) = explode('.', $name);
} }
@@ -746,7 +748,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$localKey = $localKey ?: $this->getPk(); $localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $foreignKey = $foreignKey ?: Loader::parseName(static::$name) . '_id';
return $this->relation->hasOne($model, $foreignKey, $localKey); return $this->relation->hasOne($model, $foreignKey, $localKey);
} }
@@ -780,7 +782,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$localKey = $localKey ?: $this->getPk(); $localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $foreignKey = $foreignKey ?: Loader::parseName(static::$name) . '_id';
return $this->relation->hasMany($model, $foreignKey, $localKey); return $this->relation->hasMany($model, $foreignKey, $localKey);
} }
@@ -798,9 +800,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$name = Loader::parseName(basename(str_replace('\\', '/', $model))); $name = Loader::parseName(basename(str_replace('\\', '/', $model)));
$table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); $table = $table ?: Db::name(Loader::parseName(static::$name) . '_' . $name)->getTable();
$localKey = $localKey ?: $name . '_id'; $localKey = $localKey ?: $name . '_id';
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $foreignKey = $foreignKey ?: Loader::parseName(static::$name) . '_id';
return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey); return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey);
} }
@@ -819,7 +821,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (!empty(static::$table)) { if (!empty(static::$table)) {
self::$links[$model]->table(static::$table); self::$links[$model]->table(static::$table);
} else { } else {
$name = basename(str_replace('\\', '/', $model)); $name = !empty(static::$name) ? static::$name : basename(str_replace('\\', '/', $model));
self::$links[$model]->name($name); self::$links[$model]->name($name);
} }
// 设置当前模型 确保查询返回模型对象 // 设置当前模型 确保查询返回模型对象

View File

@@ -258,7 +258,7 @@ class Query
} }
if ($lazyTime > 0) { if ($lazyTime > 0) {
// 延迟写入 // 延迟写入
$guid = md5($this->name . '_' . $field . '_' . serialize($condition)); $guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$step = $this->lazyWrite($guid, $step, $lazyTime); $step = $this->lazyWrite($guid, $step, $lazyTime);
if (empty($step)) { if (empty($step)) {
return true; // 等待下次写入 return true; // 等待下次写入
@@ -285,7 +285,7 @@ class Query
} }
if ($lazyTime > 0) { if ($lazyTime > 0) {
// 延迟写入 // 延迟写入
$guid = md5($this->name . '_' . $field . '_' . serialize($condition)); $guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$step = $this->lazyWrite($guid, -$step, $lazyTime); $step = $this->lazyWrite($guid, -$step, $lazyTime);
if (empty($step)) { if (empty($step)) {
return true; // 等待下次写入 return true; // 等待下次写入

View File

@@ -32,7 +32,7 @@ class Merge extends Model
// 设置默认外键名 仅支持单一外键 // 设置默认外键名 仅支持单一外键
if (empty($this->fk)) { if (empty($this->fk)) {
$this->fk = strtolower($this->name) . '_id'; $this->fk = strtolower(static::$name) . '_id';
} }
} }
@@ -164,7 +164,7 @@ class Merge extends Model
// 数据自动完成 // 数据自动完成
$this->autoCompleteData($this->auto); $this->autoCompleteData($this->auto);
// 处理模型数据 // 处理模型数据
$data = $this->parseData($this->name, $this->data); $data = $this->parseData(static::$name, $this->data);
self::db()->startTrans(); self::db()->startTrans();
try { try {
@@ -198,7 +198,7 @@ class Merge extends Model
} }
// 写入主表数据 // 写入主表数据
$result = self::db()->name($this->name)->strict(false)->insert($this->data); $result = self::db()->name(static::$name)->strict(false)->insert($this->data);
if ($result) { if ($result) {
$insertId = self::db()->getLastInsID(); $insertId = self::db()->getLastInsID();
// 写入外键数据 // 写入外键数据