修正model类

This commit is contained in:
thinkphp
2016-03-04 17:42:17 +08:00
parent c7be6204c9
commit e124d1f9dc
2 changed files with 28 additions and 28 deletions

View File

@@ -489,10 +489,10 @@ class Model
if (isset($options['cache'])) { if (isset($options['cache'])) {
$cache = $options['cache']; $cache = $options['cache'];
if (!isset($cache['key']) || !is_string($cache['key'])) { if (!isset($cache['key']) || !is_string($cache['key'])) {
$cache['key'] = md5(serialize($options)); $cache['key'] = md5(serialize($options));
} }
$cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null; $cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null;
$data = Cache::get($cache['key']); $data = Cache::get($cache['key']);
if (false !== $data) { if (false !== $data) {
return $data; return $data;
} }
@@ -558,7 +558,7 @@ class Model
// 根据复合主键查询 // 根据复合主键查询
$array = array_intersect_key($options, $pk); $array = array_intersect_key($options, $pk);
if (count($pk) == count($array)) { if (count($pk) == count($array)) {
$options = array_diff_key($options, $array); $options = array_diff_key($options, $array);
$options['where'] = array_combine($pk, $array); $options['where'] = array_combine($pk, $array);
} else { } else {
throw new Exception('miss complex primary data'); throw new Exception('miss complex primary data');
@@ -596,9 +596,9 @@ class Model
$field = trim($field); $field = trim($field);
// 判断查询缓存 // 判断查询缓存
if (isset($options['cache'])) { if (isset($options['cache'])) {
$cache = $options['cache']; $cache = $options['cache'];
$cache['key'] = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options)); $cache['key'] = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
$data = Cache::get($cache['key']); $data = Cache::get($cache['key']);
if (false !== $data) { if (false !== $data) {
return $data; return $data;
} }
@@ -613,9 +613,9 @@ class Model
if (is_string($resultSet)) { if (is_string($resultSet)) {
return $resultSet; return $resultSet;
} }
$field = array_keys($resultSet[0]); $field = array_keys($resultSet[0]);
$cols = []; $cols = [];
$count = count($field); $count = count($field);
foreach ($resultSet as $result) { foreach ($resultSet as $result) {
$name = $result[$field[0]]; $name = $result[$field[0]];
if (2 == $count) { if (2 == $count) {
@@ -692,7 +692,7 @@ class Model
*/ */
public function setInc($field, $step = 1, $lazyTime = 0) public function setInc($field, $step = 1, $lazyTime = 0)
{ {
$condition = $this->options['where']; $condition = !empty($this->options['where']) ? $this->options['where'] : [];
if (empty($condition)) { if (empty($condition)) {
// 没有条件不做任何更新 // 没有条件不做任何更新
throw new Exception('no data to update'); throw new Exception('no data to update');
@@ -703,8 +703,6 @@ class Model
$step = $this->lazyWrite($guid, $step, $lazyTime); $step = $this->lazyWrite($guid, $step, $lazyTime);
if (empty($step)) { if (empty($step)) {
return true; // 等待下次写入 return true; // 等待下次写入
} elseif ($step < 0) {
$step = '-' . $step;
} }
} }
return $this->setField($field, ['exp', $field . '+' . $step]); return $this->setField($field, ['exp', $field . '+' . $step]);
@@ -721,7 +719,7 @@ class Model
*/ */
public function setDec($field, $step = 1, $lazyTime = 0) public function setDec($field, $step = 1, $lazyTime = 0)
{ {
$condition = $this->options['where']; $condition = !empty($this->options['where']) ? $this->options['where'] : [];
if (empty($condition)) { if (empty($condition)) {
// 没有条件不做任何更新 // 没有条件不做任何更新
throw new Exception('no data to update'); throw new Exception('no data to update');
@@ -732,8 +730,6 @@ class Model
$step = $this->lazyWrite($guid, -$step, $lazyTime); $step = $this->lazyWrite($guid, -$step, $lazyTime);
if (empty($step)) { if (empty($step)) {
return true; // 等待下次写入 return true; // 等待下次写入
} elseif ($step > 0) {
$step = '-' . $step;
} }
} }
return $this->setField($field, ['exp', $field . '-' . $step]); return $this->setField($field, ['exp', $field . '-' . $step]);
@@ -876,10 +872,10 @@ class Model
if (isset($options['cache'])) { if (isset($options['cache'])) {
$cache = $options['cache']; $cache = $options['cache'];
if (!isset($cache['key']) || !is_string($cache['key'])) { if (!isset($cache['key']) || !is_string($cache['key'])) {
$cache['key'] = md5(serialize($options)); $cache['key'] = md5(serialize($options));
} }
$cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null; $cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null;
$data = Cache::get($cache['key']); $data = Cache::get($cache['key']);
if (false !== $data) { if (false !== $data) {
$this->data = $data; $this->data = $data;
return $data; return $data;
@@ -1000,12 +996,12 @@ class Model
if (!isset($options['value_validate'])) { if (!isset($options['value_validate'])) {
$options['value_validate'] = []; $options['value_validate'] = [];
} elseif(is_string($options['value_validate'])) { } elseif (is_string($options['value_validate'])) {
$options['value_validate'] = explode(',', $options['value_validate']); $options['value_validate'] = explode(',', $options['value_validate']);
} }
if (!isset($options['exists_validate'])) { if (!isset($options['exists_validate'])) {
$options['exists_validate'] = []; $options['exists_validate'] = [];
} elseif(is_string($options['exists_validate'])) { } elseif (is_string($options['exists_validate'])) {
$options['exists_validate'] = explode(',', $options['exists_validate']); $options['exists_validate'] = explode(',', $options['exists_validate']);
} }
foreach ($rules as $key => $rule) { foreach ($rules as $key => $rule) {
@@ -1036,7 +1032,8 @@ class Model
* @param array $value 子字段值 * @param array $value 子字段值
* @return boolean * @return boolean
*/ */
protected function fieldValidate($name, $rule, &$data, $options = [], $key = null, $value = null) { protected function fieldValidate($name, $rule, &$data, $options = [], $key = null, $value = null)
{
if (is_null($key)) { if (is_null($key)) {
$key = $name; $key = $name;
} }
@@ -1110,12 +1107,12 @@ class Model
if (!isset($options['value_fill'])) { if (!isset($options['value_fill'])) {
$options['value_fill'] = []; $options['value_fill'] = [];
} elseif(is_string($options['value_fill'])) { } elseif (is_string($options['value_fill'])) {
$options['value_fill'] = explode(',', $options['value_fill']); $options['value_fill'] = explode(',', $options['value_fill']);
} }
if (!isset($options['exists_fill'])) { if (!isset($options['exists_fill'])) {
$options['exists_fill'] = []; $options['exists_fill'] = [];
} elseif(is_string($options['exists_fill'])) { } elseif (is_string($options['exists_fill'])) {
$options['exists_fill'] = explode(',', $options['exists_fill']); $options['exists_fill'] = explode(',', $options['exists_fill']);
} }
foreach ($rules as $key => $rule) { foreach ($rules as $key => $rule) {
@@ -1243,15 +1240,15 @@ class Model
return; return;
} }
// 匿名函数 用于设置或删除表单中字段的值 // 匿名函数 用于设置或删除表单中字段的值
$dataField = function($key, $val = null) use (&$data) { $dataField = function ($key, $val = null) use (&$data) {
$str = '$data'; $str = '$data';
foreach (explode('.', $key) as $k) { foreach (explode('.', $key) as $k) {
$str .= '[\'' . $k . '\']'; $str .= '[\'' . $k . '\']';
} }
if (isset($val)) { if (isset($val)) {
eval($str . "=\$val;"); eval($str . "=\$val;");
} else { } else {
eval('unset('. $str . ');'); eval('unset(' . $str . ');');
} }
}; };
@@ -1341,14 +1338,14 @@ class Model
$result = false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options); $result = false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $options);
break; break;
case 'confirm': case 'confirm':
$result = $value == $this->getDataValue($data, $rule); $result = $this->getDataValue($data, $rule) == $value;
break; break;
case 'in': case 'in':
case 'notin': case 'notin':
$range = is_array($rule) ? $rule : explode(',', $rule); $range = is_array($rule) ? $rule : explode(',', $rule);
$result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range); $result = 'in' == $type ? in_array($value, $range) : !in_array($value, $range);
break; break;
case 'between': // 验证是否在某个范围 case 'between':// 验证是否在某个范围
case 'notbetween': // 验证是否不在某个范围 case 'notbetween': // 验证是否不在某个范围
if (is_string($rule)) { if (is_string($rule)) {
$rule = explode(',', $rule); $rule = explode(',', $rule);

View File

@@ -52,10 +52,13 @@ class langTest extends \PHPUnit_Framework_TestCase
Config::set('lang_list', ['zh-cn', 'zh-tw']); Config::set('lang_list', ['zh-cn', 'zh-tw']);
Lang::set('hello', '欢迎', 'zh-cn'); Lang::set('hello', '欢迎', 'zh-cn');
Lang::set('hello', '歡迎', 'zh-tw'); Lang::set('hello', '歡迎', 'zh-tw');
/*
Config::set('lang_detect_var', 'lang');
Config::set('lang_cookie_var', 'think_cookie');
$_GET['lang'] = 'zh-tw'; $_GET['lang'] = 'zh-tw';
Lang::detect(); Lang::detect();
$this->assertEquals('歡迎',Lang::get('hello') );*/ $this->assertEquals('歡迎', Lang::get('hello'));
$_GET['lang'] = 'zh-cn'; $_GET['lang'] = 'zh-cn';
Lang::detect(); Lang::detect();