From 5a01b29c302dda632bfa16be1abeb2510ad393fc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 May 2016 17:01:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Merge=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 83 ++++++++++++++++++++++++++++++++--- library/think/model/Merge.php | 21 +++++---- 2 files changed, 88 insertions(+), 16 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1c8e1cf7..3ad257d4 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -32,9 +32,9 @@ class Query // 当前模型类名称 protected $model; // 当前数据表名称(含前缀) - protected $table; + protected $table = ''; // 当前数据表名称(不含前缀) - protected $name; + protected $name = ''; // 查询参数 protected $options = []; // 参数绑定 @@ -43,7 +43,7 @@ class Query /** * 架构函数 * @access public - * @param object|string $connection 数据库对象实例 + * @param \think\db\Connection|string $connection 数据库对象实例 * @throws Exception */ public function __construct($connection = '', $model = '') @@ -79,6 +79,16 @@ class Query } } + /** + * 获取当前的数据库Connection对象 + * @access public + * @return \think\db\Connection + */ + public function getConnection() + { + return $this->connection; + } + /** * 执行查询 返回数据集 * @access public @@ -122,6 +132,63 @@ class Query return $this->connection->getLastInsID(); } + /** + * 执行数据库事务 + * @access public + * @param callable $callback 数据操作方法回调 + * @return mixed + */ + public function transaction($callback) + { + return $this->connection->transaction($callback); + } + + /** + * 启动事务 + * @access public + * @param string $label 事务标识 + * @return bool|null + */ + public function startTrans($label = '') + { + return $this->connection->startTrans($label); + } + + /** + * 用于非自动提交状态下面的查询提交 + * @access public + * @param string $label 事务标识 + * @return boolean + * @throws PDOException + */ + public function commit($label = '') + { + return $this->connection->commit($label); + } + + /** + * 事务回滚 + * @access public + * @return boolean + * @throws PDOException + */ + public function rollback() + { + return $this->connection->rollback(); + } + + /** + * 批处理执行SQL语句 + * 批处理的指令都认为是execute操作 + * @access public + * @param array $sql SQL批处理指令 + * @return boolean + */ + public function batchQuery($sql = []) + { + return $this->connection->batchQuery($sql); + } + /** * 获取当前的builder实例对象 * @access protected @@ -962,14 +1029,16 @@ class Query /** * 得到当前的数据表 * @access public + * @param string $name * @return string */ - public function getTable() + public function getTable($name = '') { - if (empty($this->table)) { + if ($name || empty($this->table)) { + $name = $name ?: $this->name; $tableName = $this->connection->getConfig('prefix'); - if (isset($this->name)) { - $tableName .= Loader::parseName($this->name); + if ($name) { + $tableName .= Loader::parseName($name); } } else { $tableName = $this->table; diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 5384bb64..7cf43fb4 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -66,7 +66,7 @@ class Merge extends Model foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; - $table = is_int($key) ? self::db()->name($name)->getTable() : $model; + $table = is_int($key) ? self::db()->getTable($name) : $model; $query->join($table . ' ' . $name, $name . '.' . $class->fk . '=' . $master . '.' . $class->getPk()); $fields = self::getModelField($name, $table, $class->mapFields); $query->field($fields); @@ -182,10 +182,11 @@ class Merge extends Model // 写入附表数据 foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; - $table = is_int($key) ? self::db()->name($model)->getTable() : $model; + $table = is_int($key) ? self::db()->getTable($model) : $model; // 处理关联模型数据 - $data = $this->parseData($name, $this->data); - self::db()->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data); + $data = $this->parseData($name, $this->data); + $query = clone self::db(); + $query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data); } // 新增回调 $this->trigger('after_update', $this); @@ -207,10 +208,11 @@ class Merge extends Model // 写入附表数据 foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; - $table = is_int($key) ? self::db()->name($model)->getTable() : $model; + $table = is_int($key) ? self::db()->getTable($model) : $model; // 处理关联模型数据 - $data = $this->parseData($name, $this->data, true); - self::db()->table($table)->strict(false)->insert($data); + $data = $this->parseData($name, $this->data, true); + $query = clone self::db(); + $query->table($table)->strict(false)->insert($data); } $result = $insertId; } @@ -244,8 +246,9 @@ class Merge extends Model // 删除关联数据 foreach (static::$relationModel as $key => $model) { - $table = is_int($key) ? self::db()->name($model)->getTable() : $model; - self::db()->table($table)->where($this->fk, $pk)->delete(); + $table = is_int($key) ? self::db()->getTable($model) : $model; + $query = clone self::db(); + $query->table($table)->where($this->fk, $pk)->delete(); } } $this->trigger('after_delete', $this);