From 24c928f3e232c283385e8c2cd3c156cb18807b51 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 3 Jan 2017 08:16:44 +0800 Subject: [PATCH] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0data=20inc=20de?= =?UTF-8?q?c=20exp=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=95=B0=E6=8D=AE=20insert=E5=92=8Cupdate?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=8F=82=E6=95=B0=E5=8F=AF=E4=BB=A5=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=20=E8=AF=BB=E5=8F=96data=E8=AE=BE=E7=BD=AE=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 9b30d3ed..c6586c00 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -797,6 +797,62 @@ class Query return $this; } + /** + * 设置数据 + * @access public + * @param mixed $field 字段名或者数据 + * @param mixed $value 字段值 + * @return $this + */ + public function data($field, $value = null) + { + if (is_array($field)) { + $this->options['data'] = $field; + } else { + $this->options['data'][$field] = $value; + } + return $this; + } + + /** + * 字段值增长 + * @access public + * @param string $field 字段名 + * @param integer $step 增长值 + * @return $this + */ + public function inc($field, $step = 1) + { + $this->data($field, ['exp', $field . '+' . $step]); + return $this; + } + + /** + * 字段值减少 + * @access public + * @param string $field 字段名 + * @param integer $step 增长值 + * @return $this + */ + public function dec($field, $step = 1) + { + $this->data($field, ['exp', $field . '-' . $step]); + return $this; + } + + /** + * 使用表达式设置数据 + * @access public + * @param string $field 字段名 + * @param string $value 字段值 + * @return $this + */ + public function exp($field, $value) + { + $this->data($field, ['exp', $value]); + return $this; + } + /** * 指定JOIN查询字段 * @access public @@ -1802,10 +1858,11 @@ class Query * @param string $sequence 自增序列名 * @return integer|string */ - public function insert(array $data, $replace = false, $getLastInsID = false, $sequence = null) + public function insert(array $data = [], $replace = false, $getLastInsID = false, $sequence = null) { // 分析查询表达式 $options = $this->parseExpress(); + $data = array_merge($options['data'], $data); // 生成SQL语句 $sql = $this->builder->insert($data, $options, $replace); // 获取参数绑定 @@ -1900,9 +1957,10 @@ class Query * @throws Exception * @throws PDOException */ - public function update(array $data) + public function update(array $data = []) { $options = $this->parseExpress(); + $data = array_merge($options['data'], $data); $pk = $this->getPk($options); if (isset($options['cache']) && is_string($options['cache'])) { $key = $options['cache']; @@ -2382,6 +2440,10 @@ class Query $options['field'] = '*'; } + if (!isset($options['data'])) { + $options['data'] = []; + } + if (!isset($options['strict'])) { $options['strict'] = $this->getConfig('fields_strict'); }