mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
修正关联查询的软删除数据问题
This commit is contained in:
@@ -40,6 +40,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $parent;
|
protected $parent;
|
||||||
// 数据库查询对象
|
// 数据库查询对象
|
||||||
protected $query;
|
protected $query;
|
||||||
|
protected $queryObj;
|
||||||
// 当前模型名称
|
// 当前模型名称
|
||||||
protected $name;
|
protected $name;
|
||||||
// 数据表名称
|
// 数据表名称
|
||||||
@@ -101,8 +102,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $resultSetType;
|
protected $resultSetType;
|
||||||
// 关联自动写入
|
// 关联自动写入
|
||||||
protected $relationWrite;
|
protected $relationWrite;
|
||||||
//
|
|
||||||
protected static $db;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化过的模型.
|
* 初始化过的模型.
|
||||||
@@ -125,7 +124,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 当前类名
|
// 当前类名
|
||||||
$this->class = get_class($this);
|
$this->class = get_called_class();
|
||||||
|
|
||||||
if (empty($this->name)) {
|
if (empty($this->name)) {
|
||||||
// 当前模型名
|
// 当前模型名
|
||||||
@@ -155,47 +154,83 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前模型的数据库查询对象
|
* 创建模型的查询对象
|
||||||
* @access public
|
* @access protected
|
||||||
* @param bool $baseQuery 是否调用全局查询范围
|
|
||||||
* @return Query
|
* @return Query
|
||||||
*/
|
*/
|
||||||
public function db($baseQuery = true)
|
protected function buildQuery()
|
||||||
{
|
{
|
||||||
$model = $this->class;
|
// 合并数据库配置
|
||||||
if (!isset(self::$links[$model])) {
|
if (!empty($this->connection)) {
|
||||||
// 合并数据库配置
|
if (is_array($this->connection)) {
|
||||||
if (!empty($this->connection)) {
|
$connection = array_merge(Config::get('database'), $this->connection);
|
||||||
if (is_array($this->connection)) {
|
|
||||||
$connection = array_merge(Config::get('database'), $this->connection);
|
|
||||||
} else {
|
|
||||||
$connection = $this->connection;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$connection = [];
|
$connection = $this->connection;
|
||||||
}
|
}
|
||||||
// 设置当前模型 确保查询返回模型对象
|
} else {
|
||||||
$query = Db::connect($connection)->getQuery($model, $this->query);
|
$connection = [];
|
||||||
|
|
||||||
// 设置当前数据表和模型名
|
|
||||||
if (!empty($this->table)) {
|
|
||||||
$query->setTable($this->table);
|
|
||||||
} else {
|
|
||||||
$query->name($this->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($this->pk)) {
|
|
||||||
$query->pk($this->pk);
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$links[$model] = $query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$con = Db::connect($connection);
|
||||||
|
// 设置当前模型 确保查询返回模型对象
|
||||||
|
$queryClass = $this->query ?: $con->getConfig('query');
|
||||||
|
$query = new $queryClass($con, $this->class);
|
||||||
|
$con->setQuery($query);
|
||||||
|
|
||||||
|
// 设置当前数据表和模型名
|
||||||
|
if (!empty($this->table)) {
|
||||||
|
$query->setTable($this->table);
|
||||||
|
} else {
|
||||||
|
$query->name($this->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->pk)) {
|
||||||
|
$query->pk($this->pk);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前模型的查询对象
|
||||||
|
* @access public
|
||||||
|
* @param bool $buildNewQuery 创建新的查询对象
|
||||||
|
* @return Query
|
||||||
|
*/
|
||||||
|
public function getQuery($buildNewQuery = false)
|
||||||
|
{
|
||||||
|
if (!$buildNewQuery && $this->queryObj) {
|
||||||
|
return $this->queryObj;
|
||||||
|
} else {
|
||||||
|
// 创建模型查询对象
|
||||||
|
$query = $this->buildQuery();
|
||||||
|
|
||||||
|
if (!$buildNewQuery) {
|
||||||
|
$this->queryObj = $query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前模型的数据库查询对象
|
||||||
|
* @access public
|
||||||
|
* @param bool $useBaseQuery 是否调用全局查询范围
|
||||||
|
* @param bool $buildNewQuery 创建新的查询对象
|
||||||
|
* @return Query
|
||||||
|
*/
|
||||||
|
public function db($useBaseQuery = true, $buildNewQuery = true)
|
||||||
|
{
|
||||||
|
$query = $this->getQuery($buildNewQuery);
|
||||||
|
|
||||||
// 全局作用域
|
// 全局作用域
|
||||||
if ($baseQuery && method_exists($this, 'base')) {
|
if ($useBaseQuery && method_exists($this, 'base')) {
|
||||||
call_user_func_array([$this, 'base'], [ & self::$links[$model]]);
|
call_user_func_array([$this, 'base'], [ & $query]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回当前模型的数据库查询对象
|
// 返回当前模型的数据库查询对象
|
||||||
return self::$links[$model];
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -507,7 +542,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$value = $this->parent;
|
$value = $this->parent;
|
||||||
} else {
|
} else {
|
||||||
// 首先获取关联数据
|
// 首先获取关联数据
|
||||||
$value = $modelRelation->removeOption()->getRelation();
|
$value = $modelRelation->getRelation();
|
||||||
}
|
}
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
@@ -1835,12 +1870,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
|
|
||||||
public function __call($method, $args)
|
public function __call($method, $args)
|
||||||
{
|
{
|
||||||
if (isset(static::$db)) {
|
$query = $this->db();
|
||||||
$query = static::$db;
|
|
||||||
static::$db = null;
|
|
||||||
} else {
|
|
||||||
$query = $this->db();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method_exists($this, 'scope' . $method)) {
|
if (method_exists($this, 'scope' . $method)) {
|
||||||
// 动态调用命名范围
|
// 动态调用命名范围
|
||||||
@@ -1856,13 +1886,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public static function __callStatic($method, $args)
|
public static function __callStatic($method, $args)
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
|
$query = $model->db();
|
||||||
if (isset(static::$db)) {
|
|
||||||
$query = static::$db;
|
|
||||||
static::$db = null;
|
|
||||||
} else {
|
|
||||||
$query = $model->db();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method_exists($model, 'scope' . $method)) {
|
if (method_exists($model, 'scope' . $method)) {
|
||||||
// 动态调用命名范围
|
// 动态调用命名范围
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ abstract class Connection
|
|||||||
// 监听回调
|
// 监听回调
|
||||||
protected static $event = [];
|
protected static $event = [];
|
||||||
// 查询对象
|
// 查询对象
|
||||||
protected $query = [];
|
protected $query;
|
||||||
// 使用Builder类
|
// 使用Builder类
|
||||||
protected $builder;
|
protected $builder;
|
||||||
// 数据库连接参数配置
|
// 数据库连接参数配置
|
||||||
@@ -136,20 +136,33 @@ abstract class Connection
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定当前使用的查询对象
|
||||||
|
* @access public
|
||||||
|
* @param Query $query 查询对象
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setQuery($query)
|
||||||
|
{
|
||||||
|
$this->query = $query;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建指定模型的查询对象
|
* 创建指定模型的查询对象
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $model 模型类名称
|
|
||||||
* @param string $queryClass 查询对象类名
|
|
||||||
* @return Query
|
* @return Query
|
||||||
*/
|
*/
|
||||||
public function getQuery($model = 'db', $queryClass = '')
|
public function getQuery()
|
||||||
{
|
{
|
||||||
if (!isset($this->query[$model])) {
|
if (!isset($this->query)) {
|
||||||
$class = $queryClass ?: $this->config['query'];
|
$class = $this->config['query'];
|
||||||
$this->query[$model] = new $class($this, 'db' == $model ? '' : $model);
|
|
||||||
|
$this->query = new $class($this);
|
||||||
}
|
}
|
||||||
return $this->query[$model];
|
|
||||||
|
return $this->query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1911,7 +1911,7 @@ class Query
|
|||||||
$relation = Loader::parseName($relation, 1, false);
|
$relation = Loader::parseName($relation, 1, false);
|
||||||
$model = $class->$relation();
|
$model = $class->$relation();
|
||||||
if ($model instanceof OneToOne && 0 == $model->getEagerlyType()) {
|
if ($model instanceof OneToOne && 0 == $model->getEagerlyType()) {
|
||||||
$model->removeOption()->eagerly($this, $relation, $subRelation, $closure, $first);
|
$model->eagerly($this, $relation, $subRelation, $closure, $first);
|
||||||
$first = false;
|
$first = false;
|
||||||
} elseif ($closure) {
|
} elseif ($closure) {
|
||||||
$with[$key] = $closure;
|
$with[$key] = $closure;
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ abstract class Relation
|
|||||||
protected $foreignKey;
|
protected $foreignKey;
|
||||||
// 关联表主键
|
// 关联表主键
|
||||||
protected $localKey;
|
protected $localKey;
|
||||||
// 关联查询参数
|
|
||||||
protected $option;
|
|
||||||
// 基础查询
|
// 基础查询
|
||||||
protected $baseQuery;
|
protected $baseQuery;
|
||||||
|
|
||||||
@@ -79,17 +77,6 @@ abstract class Relation
|
|||||||
return (new $this->model)->toCollection($resultSet);
|
return (new $this->model)->toCollection($resultSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 移除关联查询参数
|
|
||||||
* @access public
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function removeOption()
|
|
||||||
{
|
|
||||||
$this->query->removeOption();
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行基础查询(仅执行一次)
|
* 执行基础查询(仅执行一次)
|
||||||
* @access protected
|
* @access protected
|
||||||
@@ -105,10 +92,8 @@ abstract class Relation
|
|||||||
|
|
||||||
$result = call_user_func_array([$this->query, $method], $args);
|
$result = call_user_func_array([$this->query, $method], $args);
|
||||||
if ($result instanceof Query) {
|
if ($result instanceof Query) {
|
||||||
$this->option = $result->getOptions();
|
|
||||||
return $this;
|
return $this;
|
||||||
} else {
|
} else {
|
||||||
$this->option = [];
|
|
||||||
$this->baseQuery = false;
|
$this->baseQuery = false;
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user