diff --git a/library/think/Model.php b/library/think/Model.php index f5c5b0fb..80eb92ea 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -837,15 +837,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 + * @param array $alias 别名定义 * @return \think\db\Query|string */ - public function hasOne($model, $foreignKey = '', $localKey = '') + public function hasOne($model, $foreignKey = '', $localKey = '', $alias = []) { // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return $this->relation()->hasOne($model, $foreignKey, $localKey); + return $this->relation()->hasOne($model, $foreignKey, $localKey, $alias); } /** @@ -854,15 +855,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $otherKey 关联主键 + * @param array $alias 别名定义 * @return \think\db\Query|string */ - public function belongsTo($model, $foreignKey = '', $otherKey = '') + public function belongsTo($model, $foreignKey = '', $otherKey = '', $alias = []) { // 记录当前关联信息 $model = $this->parseModel($model); $foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $otherKey = $otherKey ?: (new $model)->getPk(); - return $this->relation()->belongsTo($model, $foreignKey, $otherKey); + return $this->relation()->belongsTo($model, $foreignKey, $otherKey, $alias); } /** @@ -871,15 +873,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 + * @param array $alias 别名定义 * @return \think\db\Query|string */ - public function hasMany($model, $foreignKey = '', $localKey = '') + public function hasMany($model, $foreignKey = '', $localKey = '', $alias = []) { // 记录当前关联信息 $model = $this->parseModel($model); $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return $this->relation()->hasMany($model, $foreignKey, $localKey); + return $this->relation()->hasMany($model, $foreignKey, $localKey, $alias); } /** @@ -889,9 +892,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string $table 中间表名 * @param string $foreignKey 关联外键 * @param string $localKey 当前模型关联键 + * @param array $alias 别名定义 * @return \think\db\Query|string */ - public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') + public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = []) { // 记录当前关联信息 $model = $this->parseModel($model); @@ -899,7 +903,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); $foreignKey = $foreignKey ?: $name . '_id'; $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; - return $this->relation()->belongsToMany($model, $table, $foreignKey, $localKey); + return $this->relation()->belongsToMany($model, $table, $foreignKey, $localKey, $alias); } /** diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1674f42c..bd1138ca 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1294,13 +1294,15 @@ class Query if (0 == $i) { $name = Loader::parseName(basename(str_replace('\\', '/', $currentModel))); $table = $this->getTable(); - $this->table($table)->alias($name)->field(true, false, $table, $name); + $alias = isset($info['alias'][$name]) ? $info['alias'][$name] : $name; + $this->table($table)->alias($alias)->field(true, false, $table, $alias); } // 预载入封装 $joinTable = $model->getTable(); $joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); - $this->via($joinName); - $this->join($joinTable . ' ' . $joinName, $name . '.' . $info['localKey'] . '=' . $joinName . '.' . $info['foreignKey'])->field(true, false, $joinTable, $joinName, $joinName . '__'); + $joinAlias = isset($info['alias'][$joinName]) ? $info['alias'][$joinName] : $joinName; + $this->via($joinAlias); + $this->join($joinTable . ' ' . $joinAlias, $alias . '.' . $info['localKey'] . '=' . $joinAlias . '.' . $info['foreignKey'])->field(true, false, $joinTable, $joinAlias, $joinName . '__'); if ($closure) { // 执行闭包查询 call_user_func_array($closure, [ & $this]); diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 2c058096..c5965743 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -36,6 +36,8 @@ class Relation protected $foreignKey; // 关联键 protected $localKey; + // 数据表别名 + protected $alias; /** * 架构函数 @@ -61,6 +63,7 @@ class Relation 'middle' => $this->middle, 'foreignKey' => $this->foreignKey, 'localKey' => $this->localKey, + 'alias' => $this->alias, ]; return $name ? $info[$name] : $info; } @@ -371,20 +374,34 @@ class Relation return $data; } + /** + * 设置当前关联定义的数据表别名 + * @access public + * @param array $alias 别名定义 + * @return $this + */ + public function setAlias($alias) + { + $this->alias = $alias; + return $this; + } + /** * HAS ONE 关联定义 * @access public * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 + * @param array $alias 别名定义 * @return $this */ - public function hasOne($model, $foreignKey, $localKey) + public function hasOne($model, $foreignKey, $localKey, $alias) { $this->type = self::HAS_ONE; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $localKey; + $this->alias = $alias; // 返回关联的模型对象 return $this; @@ -396,15 +413,17 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $otherKey 关联主键 + * @param array $alias 别名定义 * @return $this */ - public function belongsTo($model, $foreignKey, $otherKey) + public function belongsTo($model, $foreignKey, $otherKey, $alias) { // 记录当前关联信息 $this->type = self::BELONGS_TO; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $otherKey; + $this->alias = $alias; // 返回关联的模型对象 return $this; @@ -416,15 +435,17 @@ class Relation * @param string $model 模型名 * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 + * @param array $alias 别名定义 * @return $this */ - public function hasMany($model, $foreignKey, $localKey) + public function hasMany($model, $foreignKey, $localKey, $alias) { // 记录当前关联信息 $this->type = self::HAS_MANY; $this->model = $model; $this->foreignKey = $foreignKey; $this->localKey = $localKey; + $this->alias = $alias; // 返回关联的模型对象 return $this; @@ -437,9 +458,10 @@ class Relation * @param string $table 中间表名 * @param string $foreignKey 关联模型外键 * @param string $localKey 当前模型关联键 + * @param array $alias 别名定义 * @return $this */ - public function belongsToMany($model, $table, $foreignKey, $localKey) + public function belongsToMany($model, $table, $foreignKey, $localKey, $alias) { // 记录当前关联信息 $this->type = self::BELONGS_TO_MANY; @@ -447,6 +469,7 @@ class Relation $this->foreignKey = $foreignKey; $this->localKey = $localKey; $this->middle = $table; + $this->alias = $alias; // 返回关联的模型对象 return $this;