From ef9d66ce2942acf5c1124a68158edab56627a43c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 20 Nov 2018 16:26:19 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=B1=BB=20=E6=89=A9=E5=B1=95=E9=AA=8C=E8=AF=81=E8=A7=84?= =?UTF-8?q?=E5=88=99=E7=9A=84=E6=97=B6=E5=80=99=E4=B8=8D=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E6=98=AF=E5=90=A6require?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Validate.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/think/Validate.php b/src/think/Validate.php index 8b0e6f550c..4e26b2b438 100644 --- a/src/think/Validate.php +++ b/src/think/Validate.php @@ -535,11 +535,10 @@ protected function checkItem(string $field, $value, $rules, $data, string $title continue; } - if ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) { - // 验证类型 - $callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type]; - // 验证数据 - $result = call_user_func_array($callback, [$value, $rule, $data, $field, $title]); + if (isset(self::$type[$type])) { + $result = call_user_func_array(self::$type[$type], [$value, $rule, $data, $field, $title]); + } elseif ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) { + $result = call_user_func_array([$this, $type], [$value, $rule, $data, $field, $title]); } else { $result = true; } From 5941168e025bd8814de7201caae6976ae722cf3e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 21 Nov 2018 15:07:06 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E8=87=AA=E5=8A=A8=E5=86=99=E5=85=A5=20?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8DateTime=20=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E6=94=AF=E6=8C=81=E5=BE=AE=E7=A7=92=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 25 ++++++++++++++++++++++--- src/think/model/concern/Attribute.php | 24 ++++++++++-------------- src/think/model/concern/TimeStamp.php | 24 ++++++++++++++++-------- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 75b9fe9e04..75245c9f7c 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -42,6 +42,12 @@ abstract class Model implements JsonSerializable, ArrayAccess */ private $force = false; + /** + * 是否Replace + * @var bool + */ + private $replace = false; + /** * 更新条件 * @var array @@ -369,6 +375,18 @@ public function isForce(): bool return $this->force; } + /** + * 新增数据是否使用Replace + * @access public + * @param bool $replace + * @return $this + */ + public function replace(bool $replace = true) + { + $this->replace = $replace; + return $this; + } + /** * 设置数据是否存在 * @access public @@ -737,7 +755,7 @@ public function saveAll(array $dataSet, bool $replace = true): Collection if ($this->exists || (!empty($auto) && isset($data[$pk]))) { $result[$key] = self::update($data, [], $this->field); } else { - $result[$key] = self::create($data, $this->field); + $result[$key] = self::create($data, $this->field, $this->replace); } } @@ -825,9 +843,10 @@ public function auto(array $fields) * @access public * @param array $data 数据数组 * @param array $field 允许字段 + * @param bool $replace 使用Replace * @return static */ - public static function create(array $data, array $field = []): Model + public static function create(array $data, array $field = [], bool $replace = false): Model { $model = new static(); @@ -835,7 +854,7 @@ public static function create(array $data, array $field = []): Model $model->allowField($field); } - $model->isUpdate(false)->save($data); + $model->isUpdate(false)->replace($replace)->save($data); return $model; } diff --git a/src/think/model/concern/Attribute.php b/src/think/model/concern/Attribute.php index c9a210721a..a0f87d60a2 100644 --- a/src/think/model/concern/Attribute.php +++ b/src/think/model/concern/Attribute.php @@ -356,6 +356,8 @@ public function isAutoWriteTimestamp(bool $auto) */ protected function autoWriteTimestamp(string $name) { + $value = time(); + if (isset($this->type[$name])) { $type = $this->type[$name]; @@ -366,20 +368,14 @@ protected function autoWriteTimestamp(string $name) switch ($type) { case 'datetime': case 'date': - $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime(time(), $format); - break; case 'timestamp': - case 'integer': - default: - $value = time(); + $format = !empty($param) ? $param : $this->dateFormat; + $value = $this->formatDateTime($format . '.u'); break; } } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { - $value = $this->formatDateTime(time(), $this->dateFormat); - } else { - $value = $this->formatDateTime(time(), $this->dateFormat, true); + $value = $this->formatDateTime($this->dateFormat . '.u'); } return $value; @@ -426,7 +422,7 @@ protected function writeTransform($value, $type) case 'datetime': $format = !empty($param) ? $param : $this->dateFormat; $value = is_numeric($value) ? $value : strtotime($value); - $value = $this->formatDateTime($value, $format); + $value = $this->formatDateTime($format, $value); break; case 'object': if (is_object($value)) { @@ -491,9 +487,9 @@ public function getAttr(string $name, array &$item = []) 'date', 'timestamp', ])) { - $value = $this->formatDateTime(strtotime($value), $this->dateFormat); + $value = $this->formatDateTime($this->dateFormat, $value); } else { - $value = $this->formatDateTime($value, $this->dateFormat); + $value = $this->formatDateTime($this->dateFormat, $value, true); } } elseif ($notFound) { $value = $this->getRelationAttribute($name, $item); @@ -587,13 +583,13 @@ protected function readTransform($value, $type) case 'timestamp': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime($value, $format); + $value = $this->formatDateTime($format, $value, true); } break; case 'datetime': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime(strtotime($value), $format); + $value = $this->formatDateTime($format, $value); } break; case 'json': diff --git a/src/think/model/concern/TimeStamp.php b/src/think/model/concern/TimeStamp.php index cc3a4f174e..50f3b50eab 100644 --- a/src/think/model/concern/TimeStamp.php +++ b/src/think/model/concern/TimeStamp.php @@ -12,6 +12,7 @@ namespace think\model\concern; +use DateTime; /** * 自动时间戳 */ @@ -44,26 +45,33 @@ trait TimeStamp /** * 时间日期字段格式化处理 * @access protected - * @param mixed $time 时间日期表达式 * @param mixed $format 日期格式 - * @param bool $timestamp 是否进行时间戳转换 + * @param mixed $time 时间日期表达式 + * @param bool $timestamp 时间表达式是否为时间戳 * @return mixed */ - protected function formatDateTime($time, $format, bool $timestamp = false) + protected function formatDateTime($format, $time = 'now', bool $timestamp = false) { if (empty($time)) { return; } if (false === $format) { - + return $time; } elseif (false !== strpos($format, '\\')) { - $time = new $format($time); - } elseif (!$timestamp && false !== $format) { - $time = date($format, $time); + return new $format($time); + } elseif ($time instanceof DateTime) { + return $time->format($format); + } + + if ($timestamp) { + $dateTime = new DateTime(); + $dateTime->setTimestamp($time); + } else { + $dateTime = new DateTime($time); } - return $time; + return $dateTime->format($format); } /** From 0f51ec3b785a7364bf18d0d8834a041a69ee2033 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 21 Nov 2018 16:51:29 +0800 Subject: [PATCH 03/10] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0fetchArra?= =?UTF-8?q?y=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=BF=94=E5=9B=9E=E6=95=B0=E7=BB=84=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/db/Query.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/think/db/Query.php b/src/think/db/Query.php index cf18526d97..665f6cf34d 100644 --- a/src/think/db/Query.php +++ b/src/think/db/Query.php @@ -1993,6 +1993,20 @@ public function fetchCollection($collection = true) return $this; } + /** + * 设置是否返回数组 + * @access public + * @param bool $asArray 是否返回数组 + * @return $this + */ + public function fetchArray(bool $asArray = true) + { + if ($asArray) { + $this->model = null; + } + return $this; + } + /** * 设置从主服务器读取数据 * @access public From 8c040fc7a426cbf3417d92516bc419d63ed70116 Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Wed, 21 Nov 2018 08:51:50 +0000 Subject: [PATCH 04/10] Apply fixes from StyleCI --- src/think/model/concern/TimeStamp.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/think/model/concern/TimeStamp.php b/src/think/model/concern/TimeStamp.php index 50f3b50eab..dd2bad0955 100644 --- a/src/think/model/concern/TimeStamp.php +++ b/src/think/model/concern/TimeStamp.php @@ -13,6 +13,7 @@ namespace think\model\concern; use DateTime; + /** * 自动时间戳 */ From 10761a03a093ec89d2ce366cf930805dade3e518 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Nov 2018 13:17:57 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E7=B1=BB=E5=9E=8B=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 2 +- src/think/db/Connection.php | 15 ++++++++++++++- src/think/model/concern/Attribute.php | 6 ++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 75245c9f7c..2fb95c06b3 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -244,7 +244,7 @@ protected function buildQuery(): Query $query->model($this) ->name($this->name) ->json($this->json, $this->jsonAssoc) - ->setFieldType($this->type); + ->setFieldType($this->schema); if (isset(static::$readMaster['*']) || isset(static::$readMaster[static::class])) { $query->master(true); diff --git a/src/think/db/Connection.php b/src/think/db/Connection.php index 53c5d3e743..9d7c4c5390 100644 --- a/src/think/db/Connection.php +++ b/src/think/db/Connection.php @@ -135,6 +135,17 @@ abstract class Connection PDO::ATTR_EMULATE_PREPARES => false, ]; + // 参数绑定类型映射 + protected $bindType = [ + 'string' => PDO::PARAM_STR, + 'str' => PDO::PARAM_STR, + 'integer' => PDO::PARAM_INT, + 'int' => PDO::PARAM_INT, + 'boolean' => PDO::PARAM_BOOL, + 'bool' => PDO::PARAM_BOOL, + 'float' => self::PARAM_FLOAT, + ]; + // 服务器断线标识字符 protected $breakMatchStr = [ 'server has gone away', @@ -315,7 +326,9 @@ public function fieldCase(array $info): array */ public function getFieldBindType(string $type): int { - if (0 === strpos($type, 'set') || 0 === strpos($type, 'enum')) { + if (in_array($type, ['integer', 'string', 'float', 'boolean', 'bool', 'int', 'str'])) { + $bind = $this->bindType[$type]; + } elseif (0 === strpos($type, 'set') || 0 === strpos($type, 'enum')) { $bind = PDO::PARAM_STR; } elseif (preg_match('/(double|float|decimal|real|numeric)/is', $type)) { $bind = self::PARAM_FLOAT; diff --git a/src/think/model/concern/Attribute.php b/src/think/model/concern/Attribute.php index a0f87d60a2..150bf55e68 100644 --- a/src/think/model/concern/Attribute.php +++ b/src/think/model/concern/Attribute.php @@ -29,6 +29,12 @@ trait Attribute * 数据表字段信息 留空则自动获取 * @var array */ + protected $schema = []; + + /** + * 当前允许写入的字段 + * @var array + */ protected $field = []; /** From df38e8dc3166afc4e7bd7d04e0250210281bda06 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Nov 2018 13:53:27 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=92=8CDb=E7=B1=BB=E7=9A=84readMaster=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 25 ------------------------- src/think/db/Connection.php | 4 ---- src/think/db/Query.php | 25 ------------------------- 3 files changed, 54 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 2fb95c06b3..0f4e2309c1 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -102,12 +102,6 @@ abstract class Model implements JsonSerializable, ArrayAccess */ protected static $initialized = []; - /** - * 是否从主库读取(主从分布式有效) - * @var array - */ - protected static $readMaster; - /** * 查询对象实例 * @var Query @@ -216,21 +210,6 @@ public function newInstance(array $data = [], bool $isUpdate = false, $where = n return (new static($data))->isUpdate($isUpdate, $where); } - /** - * 是否从主库读取数据(主从分布有效) - * @access public - * @param bool $all 是否所有模型有效 - * @return $this - */ - public function readMaster(bool $all = false) - { - $model = $all ? '*' : static::class; - - static::$readMaster[$model] = true; - - return $this; - } - /** * 创建模型的查询对象 * @access protected @@ -246,10 +225,6 @@ protected function buildQuery(): Query ->json($this->json, $this->jsonAssoc) ->setFieldType($this->schema); - if (isset(static::$readMaster['*']) || isset(static::$readMaster[static::class])) { - $query->master(true); - } - // 设置当前数据表和模型名 if (!empty($this->table)) { $query->table($this->table); diff --git a/src/think/db/Connection.php b/src/think/db/Connection.php index 9d7c4c5390..359ac1cdf6 100644 --- a/src/think/db/Connection.php +++ b/src/think/db/Connection.php @@ -742,10 +742,6 @@ public function execute(string $sql, array $bind = [], Query $query): int // 调试结束 $this->debug(false, '', true); - if ($query && !empty($this->config['deploy']) && !empty($this->config['read_master'])) { - $query->readMaster(); - } - $this->numRows = $this->PDOStatement->rowCount(); return $this->numRows; diff --git a/src/think/db/Query.php b/src/think/db/Query.php index 665f6cf34d..4d1cb708fa 100644 --- a/src/think/db/Query.php +++ b/src/think/db/Query.php @@ -86,12 +86,6 @@ class Query */ protected static $extend = []; - /** - * 读取主库的表 - * @var array - */ - protected static $readMaster = []; - /** * 日期查询表达式 * @var array @@ -274,21 +268,6 @@ public function getConfig(string $name = '') return $this->connection->getConfig($name); } - /** - * 设置从主库读取数据 - * @access public - * @param bool $all 是否所有表有效 - * @return $this - */ - public function readMaster(bool $all = false) - { - $table = $all ? '*' : $this->getTable(); - - static::$readMaster[$table] = true; - - return $this; - } - /** * 得到当前或者指定名称的数据表 * @access public @@ -3417,10 +3396,6 @@ public function parseOptions(): array } } - if (isset(static::$readMaster['*']) || (is_string($options['table']) && isset(static::$readMaster[$options['table']]))) { - $options['master'] = true; - } - foreach (['group', 'having', 'limit', 'force', 'comment'] as $name) { if (!isset($options[$name])) { $options[$name] = ''; From 8c173f9538b811097d7a873cd68acc57f3f2e065 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Nov 2018 14:52:11 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=BF=A1=E6=81=AF=E7=9A=84=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 0f4e2309c1..30ef4463f5 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -394,7 +394,10 @@ public function isExists(): bool */ public function save(array $data = [], array $where = [], string $sequence = null): bool { - if (!$this->checkBeforeSave($data, $where)) { + $this->checkBeforeSave($data, $where); + + // 事件回调 + if (false === $this->trigger('before_write')) { return false; } @@ -419,12 +422,11 @@ public function save(array $data = [], array $where = [], string $sequence = nul * @access protected * @param array $data 数据 * @param array $where 保存条件 - * @return bool + * @return void */ - protected function checkBeforeSave(array $data, $where): bool + protected function checkBeforeSave(array $data, $where): void { if (!empty($data)) { - // 数据对象赋值 foreach ($data as $key => $value) { $this->setAttr($key, $value, $data); @@ -435,16 +437,6 @@ protected function checkBeforeSave(array $data, $where): bool $this->updateWhere = $where; } } - - // 数据自动完成 - $this->autoCompleteData($this->auto); - - // 事件回调 - if (false === $this->trigger('before_write')) { - return false; - } - - return true; } /** @@ -457,10 +449,14 @@ protected function checkAllowFields(array $append = []): array { // 检测字段 if (empty($this->field)) { - $query = $this->db(); - $table = $this->table ?: $query->getTable(); + if ($this->schema) { + $this->field = array_keys($this->schema); + } else { + $query = $this->db(); + $table = $this->table ?: $query->getTable(); - $this->field = $query->getConnection()->getTableFields($table); + $this->field = $query->getConnection()->getTableFields($table); + } $field = $this->field; } else { @@ -488,7 +484,7 @@ protected function checkAllowFields(array $append = []): array protected function updateData(array $where): bool { // 自动更新 - $this->autoCompleteData($this->update); + $this->autoCompleteData(array_merge($this->auto, $this->update)); // 事件回调 if (false === $this->trigger('before_update')) { @@ -586,7 +582,7 @@ protected function updateData(array $where): bool protected function insertData(string $sequence = null): bool { // 自动写入 - $this->autoCompleteData($this->insert); + $this->autoCompleteData(array_merge($this->auto, $this->insert)); // 时间戳自动写入 $this->checkTimeStampWrite(); From 83185b7b1cb2e8faee1dd9933e8a0e95aeab8d78 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Nov 2018 15:00:53 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=B1=BB=E7=9A=84setInc=E5=92=8CsetDec=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 56 +++++---------------------------------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 30ef4463f5..4f16b39198 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -484,7 +484,8 @@ protected function checkAllowFields(array $append = []): array protected function updateData(array $where): bool { // 自动更新 - $this->autoCompleteData(array_merge($this->auto, $this->update)); + $auto = array_merge($this->auto, $this->update); + $this->autoCompleteData($auto); // 事件回调 if (false === $this->trigger('before_update')) { @@ -513,7 +514,7 @@ protected function updateData(array $where): bool } // 检查允许字段 - $allowFields = $this->checkAllowFields(array_merge($this->auto, $this->update)); + $allowFields = $this->checkAllowFields($auto); // 保留主键数据 foreach ($this->data as $key => $val) { @@ -582,7 +583,8 @@ protected function updateData(array $where): bool protected function insertData(string $sequence = null): bool { // 自动写入 - $this->autoCompleteData(array_merge($this->auto, $this->insert)); + $auto = array_merge($this->auto, $this->insert); + $this->autoCompleteData($auto); // 时间戳自动写入 $this->checkTimeStampWrite(); @@ -592,7 +594,7 @@ protected function insertData(string $sequence = null): bool } // 检查允许字段 - $allowFields = $this->checkAllowFields(array_merge($this->auto, $this->insert)); + $allowFields = $this->checkAllowFields($auto); $db = $this->db(); $db->startTrans(); @@ -633,52 +635,6 @@ protected function insertData(string $sequence = null): bool } } - /** - * 字段值(延迟)增长 - * @access public - * @param string $field 字段名 - * @param integer $step 增长值 - * @param integer $lazyTime 延时时间(s) - * @return integer|true - * @throws Exception - */ - public function setInc(string $field, int $step = 1, int $lazyTime = 0) - { - // 读取更新条件 - $where = $this->getWhere(); - - $result = $this->where($where)->setInc($field, $step, $lazyTime); - - if (true !== $result) { - $this->data[$field] += $step; - } - - return $result; - } - - /** - * 字段值(延迟)减少 - * @access public - * @param string $field 字段名 - * @param integer $step 减少值 - * @param integer $lazyTime 延时时间(s) - * @return integer|true - * @throws Exception - */ - public function setDec(string $field, int $step = 1, int $lazyTime = 0) - { - // 读取更新条件 - $where = $this->getWhere(); - - $result = $this->where($where)->setDec($field, $step, $lazyTime); - - if (true !== $result) { - $this->data[$field] -= $step; - } - - return $result; - } - /** * 获取当前的更新条件 * @access protected From d58ec1bb00ea27c7e0fd99e1a76149801295588b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Nov 2018 15:28:52 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E5=BA=9F=E5=BC=83save=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84where=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/Model.php | 61 ++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/src/think/Model.php b/src/think/Model.php index 4f16b39198..0f25b8016b 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -388,20 +388,22 @@ public function isExists(): bool * 保存当前数据对象 * @access public * @param array $data 数据 - * @param array $where 更新条件 * @param string $sequence 自增序列名 * @return bool */ - public function save(array $data = [], array $where = [], string $sequence = null): bool + public function save(array $data = [], string $sequence = null): bool { - $this->checkBeforeSave($data, $where); + // 数据对象赋值 + foreach ($data as $key => $value) { + $this->setAttr($key, $value, $data); + } // 事件回调 if (false === $this->trigger('before_write')) { return false; } - $result = $this->exists ? $this->updateData($where) : $this->insertData($sequence); + $result = $this->exists ? $this->updateData() : $this->insertData($sequence); if (false === $result) { return false; @@ -417,28 +419,6 @@ public function save(array $data = [], array $where = [], string $sequence = nul return true; } - /** - * 写入之前检查数据 - * @access protected - * @param array $data 数据 - * @param array $where 保存条件 - * @return void - */ - protected function checkBeforeSave(array $data, $where): void - { - if (!empty($data)) { - // 数据对象赋值 - foreach ($data as $key => $value) { - $this->setAttr($key, $value, $data); - } - - if (!empty($where)) { - $this->exists = true; - $this->updateWhere = $where; - } - } - } - /** * 检查数据是否允许写入 * @access protected @@ -478,13 +458,13 @@ protected function checkAllowFields(array $append = []): array /** * 保存写入数据 * @access protected - * @param array $where 保存条件 * @return bool */ - protected function updateData(array $where): bool + protected function updateData(): bool { // 自动更新 $auto = array_merge($this->auto, $this->update); + $this->autoCompleteData($auto); // 事件回调 @@ -509,10 +489,6 @@ protected function updateData(array $where): bool $this->data[$this->updateTime] = $data[$this->updateTime]; } - if (empty($where) && !empty($this->updateWhere)) { - $where = $this->updateWhere; - } - // 检查允许字段 $allowFields = $this->checkAllowFields($auto); @@ -535,14 +511,18 @@ protected function updateData(array $where): bool if (!empty($array)) { $where = $array; + } else { + $where = $this->updateWhere; } foreach ((array) $this->relationWrite as $name => $val) { - if (is_array($val)) { - foreach ($val as $key) { - if (isset($data[$key])) { - unset($data[$key]); - } + if (!is_array($val)) { + continue; + } + + foreach ($val as $key) { + if (isset($data[$key])) { + unset($data[$key]); } } } @@ -584,6 +564,7 @@ protected function insertData(string $sequence = null): bool { // 自动写入 $auto = array_merge($this->auto, $this->insert); + $this->autoCompleteData($auto); // 时间戳自动写入 @@ -680,7 +661,7 @@ public function saveAll(array $dataSet, bool $replace = true): Collection foreach ($dataSet as $key => $data) { if ($this->exists || (!empty($auto) && isset($data[$pk]))) { - $result[$key] = self::update($data, [], $this->field); + $result[$key] = self::update($data, $this->field); } else { $result[$key] = self::create($data, $this->field, $this->replace); } @@ -794,7 +775,7 @@ public static function create(array $data, array $field = [], bool $replace = fa * @param array $field 允许字段 * @return static */ - public static function update(array $data, array $where = [], array $field = []) + public static function update(array $data, array $field = []) { $model = new static(); @@ -802,7 +783,7 @@ public static function update(array $data, array $where = [], array $field = []) $model->allowField($field); } - $model->isUpdate(true)->save($data, $where); + $model->isUpdate(true)->save($data); return $model; } From 8dfe8eb9ef5e5e5aa58fdf59b31a5713d27a285a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Nov 2018 16:00:48 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/think/App.php | 2 +- src/think/Model.php | 1 - src/think/db/Query.php | 52 ++++++++++++++---------------------------- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/think/App.php b/src/think/App.php index 3da3b06e87..642583451b 100644 --- a/src/think/App.php +++ b/src/think/App.php @@ -21,7 +21,7 @@ */ class App extends Container { - const VERSION = '5.2.0beta1'; + const VERSION = '5.2.0beta2'; /** * 应用名称 diff --git a/src/think/Model.php b/src/think/Model.php index 0f25b8016b..ddf6559c08 100644 --- a/src/think/Model.php +++ b/src/think/Model.php @@ -771,7 +771,6 @@ public static function create(array $data, array $field = [], bool $replace = fa * 更新数据 * @access public * @param array $data 数据数组 - * @param array $where 更新条件 * @param array $field 允许字段 * @return static */ diff --git a/src/think/db/Query.php b/src/think/db/Query.php index 4d1cb708fa..d659a66652 100644 --- a/src/think/db/Query.php +++ b/src/think/db/Query.php @@ -833,32 +833,23 @@ protected function getJoinTable($join, &$alias = null) if (is_array($join)) { $table = $join; $alias = array_shift($join); - } else { - $join = trim($join); + return $table; + } - if (false !== strpos($join, '(')) { - // 使用子查询 - $table = $join; - } else { - $prefix = $this->prefix; - if (strpos($join, ' ')) { - // 使用别名 - list($table, $alias) = explode(' ', $join); - } else { - $table = $join; - if (false === strpos($join, '.') && 0 !== strpos($join, '__')) { - $alias = $join; - } - } + $join = trim($join); - if ($prefix && false === strpos($table, '.') && 0 !== strpos($table, $prefix) && 0 !== strpos($table, '__')) { - $table = $this->getTable($table); - } - } + if (false !== strpos($join, '(')) { + // 使用子查询 + $table = $join; + } elseif (strpos($join, ' ')) { + // 使用别名 + list($table, $alias) = explode(' ', $join); + } else { + $table = $join; + } - if (isset($alias) && $table != $alias) { - $table = [$table => $alias]; - } + if (isset($alias) && $table != $alias) { + $table = [$table => $alias]; } return $table; @@ -1673,7 +1664,7 @@ public function table($table) if (is_string($table)) { if (strpos($table, ')')) { // 子查询 - } elseif (strpos($table, ',')) { + } else { $tables = explode(',', $table); $table = []; @@ -1686,11 +1677,6 @@ public function table($table) $table[] = $item; } } - } elseif (strpos($table, ' ')) { - list($table, $alias) = explode(' ', $table); - - $table = [$table => $alias]; - $this->alias($table); } } else { $tables = $table; @@ -1791,7 +1777,7 @@ public function orderRaw(string $field, array $bind = []) } /** - * 指定Field排序 order('id',[1,2,3],'desc') + * 指定Field排序 orderField('id',[1,2,3],'desc') * @access public * @param string $field 排序字段 * @param array $values 排序值 @@ -1907,11 +1893,7 @@ public function alias($alias) if (is_array($alias)) { $this->options['alias'] = $alias; } else { - if (isset($this->options['table'])) { - $table = is_array($this->options['table']) ? key($this->options['table']) : $this->options['table']; - } else { - $table = $this->getTable(); - } + $table = $this->getTable(); $this->options['alias'][$table] = $alias; }