diff --git a/library/think/Model.php b/library/think/Model.php index e617b646..5d112e44 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -192,7 +192,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $con = Db::connect($connection); // 设置当前模型 确保查询返回模型对象 $queryClass = $this->query ?: $con->getConfig('query'); - $query = new $queryClass($con, $this->class); + $query = new $queryClass($con, $this); // 设置当前数据表和模型名 if (!empty($this->table)) { @@ -208,6 +208,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $query; } + /** + * 创建新的模型实例 + * @access public + * @param array|object $data 数据 + * @param bool $isUpdate 是否为更新 + * @param mixed $where 更新条件 + * @return Model + */ + public function newInstance($data = [], $isUpdate = false, $where = null) + { + return (new static($data))->isUpdate($isUpdate, $where); + } + /** * 获取当前模型的查询对象 * @access public @@ -599,7 +612,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ protected function getRelationData(Relation $modelRelation) { - if ($this->parent && get_class($this->parent) == $modelRelation->getModel()) { + if ($this->parent && $modelRelation->getModel() == $this->parent) { $value = $this->parent; } else { // 首先获取关联数据 diff --git a/library/think/db/Query.php b/library/think/db/Query.php index b3d365a7..8ee6e15c 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -58,9 +58,9 @@ class Query * 构造函数 * @access public * @param Connection $connection 数据库对象实例 - * @param string $model 模型名 + * @param Model $model 模型对象 */ - public function __construct(Connection $connection = null, $model = '') + public function __construct(Connection $connection = null, $model = null) { $this->connection = $connection ?: Db::connect([], true); $this->prefix = $this->connection->getConfig('prefix'); @@ -133,7 +133,7 @@ class Query /** * 获取当前的模型对象名 * @access public - * @return string + * @return Model|null */ public function getModel() { @@ -1910,11 +1910,10 @@ class Query $with = explode(',', $with); } - $first = true; - $currentModel = $this->model; + $first = true; /** @var Model $class */ - $class = new $currentModel; + $class = $this->model; foreach ($with as $key => $relation) { $subRelation = ''; $closure = false; @@ -1973,7 +1972,7 @@ class Query $relation = $key; } $relation = Loader::parseName($relation, 1, false); - $count = '(' . (new $this->model)->$relation()->getRelationCountQuery($closure) . ')'; + $count = '(' . $this->model->$relation()->getRelationCountQuery($closure) . ')'; $this->field([$count => Loader::parseName($relation) . '_count']); } } @@ -2365,11 +2364,10 @@ class Query // 数据列表读取后的处理 if (!empty($this->model)) { // 生成模型对象 - $modelName = $this->model; if (count($resultSet) > 0) { foreach ($resultSet as $key => $result) { /** @var Model $model */ - $model = new $modelName($result); + $model = $this->model->newInstance($result); $model->isUpdate(true); // 关联查询 @@ -2389,7 +2387,7 @@ class Query // 模型数据集转换 $resultSet = $model->toCollection($resultSet); } else { - $resultSet = (new $modelName)->toCollection($resultSet); + $resultSet = $this->model->toCollection($resultSet); } } elseif ('collection' == $this->connection->getConfig('resultset_type')) { // 返回Collection对象 @@ -2525,8 +2523,7 @@ class Query if (!empty($result)) { if (!empty($this->model)) { // 返回模型对象 - $model = $this->model; - $result = new $model($result); + $result = $this->model->newInstance($result); $result->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); // 关联查询 if (!empty($options['relation'])) { @@ -2557,7 +2554,8 @@ class Query protected function throwNotFound($options = []) { if (!empty($this->model)) { - throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options); + $class = get_class($this->model); + throw new ModelNotFoundException('model data Not Found:' . $class, $class, $options); } else { $table = is_array($options['table']) ? key($options['table']) : $options['table']; throw new DataNotFoundException('table data not Found:' . $table, $table, $options); diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 21cbdfd8..f4c5781e 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -49,11 +49,11 @@ abstract class Relation /** * 获取当前的关联模型类 * @access public - * @return string + * @return Model */ public function getModel() { - return $this->model; + return $this->query->getModel(); } /** diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 647fa8e9..02d7d563 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -57,18 +57,18 @@ abstract class OneToOne extends Relation */ public function eagerly(Query $query, $relation, $subRelation, $closure, $first) { - $name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel()))); - $alias = $name; + $name = Loader::parseName(basename(str_replace('\\', '/', get_class($query->getModel())))); + if ($first) { $table = $query->getTable(); - $query->table([$table => $alias]); + $query->table([$table => $name]); if ($query->getOptions('field')) { $field = $query->getOptions('field'); $query->removeOption('field'); } else { $field = true; } - $query->field($field, false, $table, $alias); + $query->field($field, false, $table, $name); $field = null; } @@ -78,9 +78,9 @@ abstract class OneToOne extends Relation $query->via($joinAlias); if ($this instanceof BelongsTo) { - $query->join([$joinTable => $joinAlias], $alias . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType); + $query->join([$joinTable => $joinAlias], $name . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType); } else { - $query->join([$joinTable => $joinAlias], $alias . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType); + $query->join([$joinTable => $joinAlias], $name . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType); } if ($closure) {