取消关联类无效的alias参数 仅morphTo关联保留 用于设置多态别名

This commit is contained in:
thinkphp
2017-01-16 23:03:12 +08:00
parent 908de8f4ee
commit 1568af03d8
9 changed files with 31 additions and 44 deletions

View File

@@ -1383,7 +1383,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $model 模型名
* @param string $foreignKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
* @param array $alias 别名定义(已经废弃)
* @param string $joinType JOIN类型
* @return HasOne
*/
@@ -1393,7 +1393,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = $this->parseModel($model);
$localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
return new HasOne($this, $model, $foreignKey, $localKey, $alias, $joinType);
return new HasOne($this, $model, $foreignKey, $localKey, $joinType);
}
/**
@@ -1402,7 +1402,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $model 模型名
* @param string $foreignKey 关联外键
* @param string $otherKey 关联主键
* @param array $alias 别名定义
* @param array $alias 别名定义(已经废弃)
* @param string $joinType JOIN类型
* @return BelongsTo
*/
@@ -1412,7 +1412,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = $this->parseModel($model);
$foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id';
$otherKey = $otherKey ?: (new $model)->getPk();
return new BelongsTo($this, $model, $foreignKey, $otherKey, $alias, $joinType);
return new BelongsTo($this, $model, $foreignKey, $otherKey, $joinType);
}
/**
@@ -1421,16 +1421,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $model 模型名
* @param string $foreignKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
* @return HasMany
*/
public function hasMany($model, $foreignKey = '', $localKey = '', $alias = [])
public function hasMany($model, $foreignKey = '', $localKey = '')
{
// 记录当前关联信息
$model = $this->parseModel($model);
$localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
return new HasMany($this, $model, $foreignKey, $localKey, $alias);
return new HasMany($this, $model, $foreignKey, $localKey);
}
/**
@@ -1441,10 +1440,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $foreignKey 关联外键
* @param string $throughKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
* @return HasManyThrough
*/
public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $alias = [])
public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '')
{
// 记录当前关联信息
$model = $this->parseModel($model);
@@ -1453,7 +1451,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
$name = Loader::parseName(basename(str_replace('\\', '/', $through)));
$throughKey = $throughKey ?: $name . '_id';
return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $alias);
return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey);
}
/**
@@ -1463,10 +1461,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param string $table 中间表名
* @param string $foreignKey 关联外键
* @param string $localKey 当前模型关联键
* @param array $alias 别名定义
* @return BelongsToMany
*/
public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = [])
public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '')
{
// 记录当前关联信息
$model = $this->parseModel($model);
@@ -1474,7 +1471,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$table = $table ?: $this->db(false)->getTable(Loader::parseName($this->name) . '_' . $name);
$foreignKey = $foreignKey ?: $name . '_id';
$localKey = $localKey ?: Loader::parseName($this->name) . '_id';
return new BelongsToMany($this, $model, $table, $foreignKey, $localKey, $alias);
return new BelongsToMany($this, $model, $table, $foreignKey, $localKey);
}
/**

View File

@@ -25,8 +25,6 @@ abstract class Relation
protected $foreignKey;
// 关联表主键
protected $localKey;
// 数据表别名
protected $alias;
// 当前关联的JOIN类型
protected $joinType;
// 关联模型查询对象
@@ -80,18 +78,6 @@ abstract class Relation
return $class ? new $class($resultSet) : $resultSet;
}
/**
* 设置当前关联定义的数据表别名
* @access public
* @param array $alias 别名定义
* @return $this
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* 移除关联查询参数
* @access public

View File

@@ -22,16 +22,14 @@ class BelongsTo extends OneToOne
* @param string $model 模型名
* @param string $foreignKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
* @param string $joinType JOIN类型
*/
public function __construct(Model $parent, $model, $foreignKey, $localKey, $alias = [], $joinType = 'INNER')
public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER')
{
$this->parent = $parent;
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->alias = $alias;
$this->joinType = $joinType;
$this->query = (new $model)->db();
}

View File

@@ -31,16 +31,14 @@ class BelongsToMany extends Relation
* @param string $table 中间表名
* @param string $foreignKey 关联模型外键
* @param string $localKey 当前模型关联键
* @param array $alias 别名定义
*/
public function __construct(Model $parent, $model, $table, $foreignKey, $localKey, $alias = [])
public function __construct(Model $parent, $model, $table, $foreignKey, $localKey)
{
$this->parent = $parent;
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->middle = $table;
$this->alias = $alias;
$this->query = (new $model)->db();
}

View File

@@ -25,15 +25,13 @@ class HasMany extends Relation
* @param string $model 模型名
* @param string $foreignKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
*/
public function __construct(Model $parent, $model, $foreignKey, $localKey, $alias = [])
public function __construct(Model $parent, $model, $foreignKey, $localKey)
{
$this->parent = $parent;
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->alias = $alias;
$this->query = (new $model)->db();
}

View File

@@ -33,9 +33,8 @@ class HasManyThrough extends Relation
* @param string $firstkey 关联外键
* @param string $secondKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
*/
public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey, $alias = [])
public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey)
{
$this->parent = $parent;
$this->model = $model;
@@ -43,7 +42,6 @@ class HasManyThrough extends Relation
$this->foreignKey = $foreignKey;
$this->throughKey = $throughKey;
$this->localKey = $localKey;
$this->alias = $alias;
$this->query = (new $model)->db();
}

View File

@@ -22,16 +22,14 @@ class HasOne extends OneToOne
* @param string $model 模型名
* @param string $foreignKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
* @param string $joinType JOIN类型
*/
public function __construct(Model $parent, $model, $foreignKey, $localKey, $alias = [], $joinType = 'INNER')
public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER')
{
$this->parent = $parent;
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->alias = $alias;
$this->joinType = $joinType;
$this->query = (new $model)->db();
}

View File

@@ -20,6 +20,8 @@ class MorphTo extends Relation
// 多态字段
protected $morphKey;
protected $morphType;
// 多态别名
protected $alias;
/**
* 架构函数
@@ -74,6 +76,18 @@ class MorphTo extends Relation
return $model;
}
/**
* 设置多态别名
* @access public
* @param array $alias 别名定义
* @return $this
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* 预载入关联查询
* @access public

View File

@@ -37,7 +37,7 @@ abstract class OneToOne extends Relation
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
{
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
$alias = isset($this->alias[$name]) ? $this->alias[$name] : $name;
$alias = $name;
if ($first) {
$table = $query->getTable();
$query->table([$table => $alias]);
@@ -53,7 +53,7 @@ abstract class OneToOne extends Relation
// 预载入封装
$joinTable = $this->query->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
$joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation;
$joinAlias = $relation;
$query->via($joinAlias);
if ($this instanceof BelongsTo) {