修正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

@@ -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]);
@@ -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,7 +1240,7 @@ 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 . '\']';
@@ -1251,7 +1248,7 @@ class Model
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();