关联定义支持数据表别名定义

This commit is contained in:
thinkphp
2016-05-24 12:29:51 +08:00
parent 38f343f69f
commit 165c72e0aa
3 changed files with 44 additions and 15 deletions

View File

@@ -837,15 +837,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
* @param array $alias 别名定义
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
public function hasOne($model, $foreignKey = '', $localKey = '') public function hasOne($model, $foreignKey = '', $localKey = '', $alias = [])
{ {
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$localKey = $localKey ?: $this->getPk(); $localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $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 $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $otherKey 关联主键 * @param string $otherKey 关联主键
* @param array $alias 别名定义
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
public function belongsTo($model, $foreignKey = '', $otherKey = '') public function belongsTo($model, $foreignKey = '', $otherKey = '', $alias = [])
{ {
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id';
$otherKey = $otherKey ?: (new $model)->getPk(); $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 $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
* @param array $alias 别名定义
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
public function hasMany($model, $foreignKey = '', $localKey = '') public function hasMany($model, $foreignKey = '', $localKey = '', $alias = [])
{ {
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$localKey = $localKey ?: $this->getPk(); $localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $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 $table 中间表名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 当前模型关联键 * @param string $localKey 当前模型关联键
* @param array $alias 别名定义
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = [])
{ {
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
@@ -899,7 +903,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable();
$foreignKey = $foreignKey ?: $name . '_id'; $foreignKey = $foreignKey ?: $name . '_id';
$localKey = $localKey ?: Loader::parseName($this->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);
} }
/** /**

View File

@@ -1294,13 +1294,15 @@ class Query
if (0 == $i) { if (0 == $i) {
$name = Loader::parseName(basename(str_replace('\\', '/', $currentModel))); $name = Loader::parseName(basename(str_replace('\\', '/', $currentModel)));
$table = $this->getTable(); $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(); $joinTable = $model->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); $joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model'])));
$this->via($joinName); $joinAlias = isset($info['alias'][$joinName]) ? $info['alias'][$joinName] : $joinName;
$this->join($joinTable . ' ' . $joinName, $name . '.' . $info['localKey'] . '=' . $joinName . '.' . $info['foreignKey'])->field(true, false, $joinTable, $joinName, $joinName . '__'); $this->via($joinAlias);
$this->join($joinTable . ' ' . $joinAlias, $alias . '.' . $info['localKey'] . '=' . $joinAlias . '.' . $info['foreignKey'])->field(true, false, $joinTable, $joinAlias, $joinName . '__');
if ($closure) { if ($closure) {
// 执行闭包查询 // 执行闭包查询
call_user_func_array($closure, [ & $this]); call_user_func_array($closure, [ & $this]);

View File

@@ -36,6 +36,8 @@ class Relation
protected $foreignKey; protected $foreignKey;
// 关联键 // 关联键
protected $localKey; protected $localKey;
// 数据表别名
protected $alias;
/** /**
* 架构函数 * 架构函数
@@ -61,6 +63,7 @@ class Relation
'middle' => $this->middle, 'middle' => $this->middle,
'foreignKey' => $this->foreignKey, 'foreignKey' => $this->foreignKey,
'localKey' => $this->localKey, 'localKey' => $this->localKey,
'alias' => $this->alias,
]; ];
return $name ? $info[$name] : $info; return $name ? $info[$name] : $info;
} }
@@ -371,20 +374,34 @@ class Relation
return $data; return $data;
} }
/**
* 设置当前关联定义的数据表别名
* @access public
* @param array $alias 别名定义
* @return $this
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/** /**
* HAS ONE 关联定义 * HAS ONE 关联定义
* @access public * @access public
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
* @param array $alias 别名定义
* @return $this * @return $this
*/ */
public function hasOne($model, $foreignKey, $localKey) public function hasOne($model, $foreignKey, $localKey, $alias)
{ {
$this->type = self::HAS_ONE; $this->type = self::HAS_ONE;
$this->model = $model; $this->model = $model;
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
$this->alias = $alias;
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
@@ -396,15 +413,17 @@ class Relation
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $otherKey 关联主键 * @param string $otherKey 关联主键
* @param array $alias 别名定义
* @return $this * @return $this
*/ */
public function belongsTo($model, $foreignKey, $otherKey) public function belongsTo($model, $foreignKey, $otherKey, $alias)
{ {
// 记录当前关联信息 // 记录当前关联信息
$this->type = self::BELONGS_TO; $this->type = self::BELONGS_TO;
$this->model = $model; $this->model = $model;
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $otherKey; $this->localKey = $otherKey;
$this->alias = $alias;
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
@@ -416,15 +435,17 @@ class Relation
* @param string $model 模型名 * @param string $model 模型名
* @param string $foreignKey 关联外键 * @param string $foreignKey 关联外键
* @param string $localKey 关联主键 * @param string $localKey 关联主键
* @param array $alias 别名定义
* @return $this * @return $this
*/ */
public function hasMany($model, $foreignKey, $localKey) public function hasMany($model, $foreignKey, $localKey, $alias)
{ {
// 记录当前关联信息 // 记录当前关联信息
$this->type = self::HAS_MANY; $this->type = self::HAS_MANY;
$this->model = $model; $this->model = $model;
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
$this->alias = $alias;
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;
@@ -437,9 +458,10 @@ class Relation
* @param string $table 中间表名 * @param string $table 中间表名
* @param string $foreignKey 关联模型外键 * @param string $foreignKey 关联模型外键
* @param string $localKey 当前模型关联键 * @param string $localKey 当前模型关联键
* @param array $alias 别名定义
* @return $this * @return $this
*/ */
public function belongsToMany($model, $table, $foreignKey, $localKey) public function belongsToMany($model, $table, $foreignKey, $localKey, $alias)
{ {
// 记录当前关联信息 // 记录当前关联信息
$this->type = self::BELONGS_TO_MANY; $this->type = self::BELONGS_TO_MANY;
@@ -447,6 +469,7 @@ class Relation
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->localKey = $localKey; $this->localKey = $localKey;
$this->middle = $table; $this->middle = $table;
$this->alias = $alias;
// 返回关联的模型对象 // 返回关联的模型对象
return $this; return $this;