修正Merge模型

This commit is contained in:
thinkphp
2016-05-17 17:01:04 +08:00
parent 2072a395a6
commit 5a01b29c30
2 changed files with 88 additions and 16 deletions

View File

@@ -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;

View File

@@ -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);