mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
取消关联类无效的alias参数 仅morphTo关联保留 用于设置多态别名
This commit is contained in:
@@ -1383,7 +1383,7 @@ 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 别名定义
|
* @param array $alias 别名定义(已经废弃)
|
||||||
* @param string $joinType JOIN类型
|
* @param string $joinType JOIN类型
|
||||||
* @return HasOne
|
* @return HasOne
|
||||||
*/
|
*/
|
||||||
@@ -1393,7 +1393,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$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 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 $model 模型名
|
||||||
* @param string $foreignKey 关联外键
|
* @param string $foreignKey 关联外键
|
||||||
* @param string $otherKey 关联主键
|
* @param string $otherKey 关联主键
|
||||||
* @param array $alias 别名定义
|
* @param array $alias 别名定义(已经废弃)
|
||||||
* @param string $joinType JOIN类型
|
* @param string $joinType JOIN类型
|
||||||
* @return BelongsTo
|
* @return BelongsTo
|
||||||
*/
|
*/
|
||||||
@@ -1412,7 +1412,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$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 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 $model 模型名
|
||||||
* @param string $foreignKey 关联外键
|
* @param string $foreignKey 关联外键
|
||||||
* @param string $localKey 关联主键
|
* @param string $localKey 关联主键
|
||||||
* @param array $alias 别名定义
|
|
||||||
* @return HasMany
|
* @return HasMany
|
||||||
*/
|
*/
|
||||||
public function hasMany($model, $foreignKey = '', $localKey = '', $alias = [])
|
public function hasMany($model, $foreignKey = '', $localKey = '')
|
||||||
{
|
{
|
||||||
// 记录当前关联信息
|
// 记录当前关联信息
|
||||||
$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 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 $foreignKey 关联外键
|
||||||
* @param string $throughKey 关联外键
|
* @param string $throughKey 关联外键
|
||||||
* @param string $localKey 关联主键
|
* @param string $localKey 关联主键
|
||||||
* @param array $alias 别名定义
|
|
||||||
* @return HasManyThrough
|
* @return HasManyThrough
|
||||||
*/
|
*/
|
||||||
public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $alias = [])
|
public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '')
|
||||||
{
|
{
|
||||||
// 记录当前关联信息
|
// 记录当前关联信息
|
||||||
$model = $this->parseModel($model);
|
$model = $this->parseModel($model);
|
||||||
@@ -1453,7 +1451,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
|
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
|
||||||
$name = Loader::parseName(basename(str_replace('\\', '/', $through)));
|
$name = Loader::parseName(basename(str_replace('\\', '/', $through)));
|
||||||
$throughKey = $throughKey ?: $name . '_id';
|
$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 $table 中间表名
|
||||||
* @param string $foreignKey 关联外键
|
* @param string $foreignKey 关联外键
|
||||||
* @param string $localKey 当前模型关联键
|
* @param string $localKey 当前模型关联键
|
||||||
* @param array $alias 别名定义
|
|
||||||
* @return BelongsToMany
|
* @return BelongsToMany
|
||||||
*/
|
*/
|
||||||
public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = [])
|
public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '')
|
||||||
{
|
{
|
||||||
// 记录当前关联信息
|
// 记录当前关联信息
|
||||||
$model = $this->parseModel($model);
|
$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);
|
$table = $table ?: $this->db(false)->getTable(Loader::parseName($this->name) . '_' . $name);
|
||||||
$foreignKey = $foreignKey ?: $name . '_id';
|
$foreignKey = $foreignKey ?: $name . '_id';
|
||||||
$localKey = $localKey ?: Loader::parseName($this->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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ abstract class Relation
|
|||||||
protected $foreignKey;
|
protected $foreignKey;
|
||||||
// 关联表主键
|
// 关联表主键
|
||||||
protected $localKey;
|
protected $localKey;
|
||||||
// 数据表别名
|
|
||||||
protected $alias;
|
|
||||||
// 当前关联的JOIN类型
|
// 当前关联的JOIN类型
|
||||||
protected $joinType;
|
protected $joinType;
|
||||||
// 关联模型查询对象
|
// 关联模型查询对象
|
||||||
@@ -80,18 +78,6 @@ abstract class Relation
|
|||||||
return $class ? new $class($resultSet) : $resultSet;
|
return $class ? new $class($resultSet) : $resultSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置当前关联定义的数据表别名
|
|
||||||
* @access public
|
|
||||||
* @param array $alias 别名定义
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setAlias($alias)
|
|
||||||
{
|
|
||||||
$this->alias = $alias;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 移除关联查询参数
|
* 移除关联查询参数
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
@@ -22,16 +22,14 @@ class BelongsTo extends OneToOne
|
|||||||
* @param string $model 模型名
|
* @param string $model 模型名
|
||||||
* @param string $foreignKey 关联外键
|
* @param string $foreignKey 关联外键
|
||||||
* @param string $localKey 关联主键
|
* @param string $localKey 关联主键
|
||||||
* @param array $alias 别名定义
|
|
||||||
* @param string $joinType JOIN类型
|
* @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->parent = $parent;
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
$this->foreignKey = $foreignKey;
|
$this->foreignKey = $foreignKey;
|
||||||
$this->localKey = $localKey;
|
$this->localKey = $localKey;
|
||||||
$this->alias = $alias;
|
|
||||||
$this->joinType = $joinType;
|
$this->joinType = $joinType;
|
||||||
$this->query = (new $model)->db();
|
$this->query = (new $model)->db();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,16 +31,14 @@ class BelongsToMany extends Relation
|
|||||||
* @param string $table 中间表名
|
* @param string $table 中间表名
|
||||||
* @param string $foreignKey 关联模型外键
|
* @param string $foreignKey 关联模型外键
|
||||||
* @param string $localKey 当前模型关联键
|
* @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->parent = $parent;
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
$this->foreignKey = $foreignKey;
|
$this->foreignKey = $foreignKey;
|
||||||
$this->localKey = $localKey;
|
$this->localKey = $localKey;
|
||||||
$this->middle = $table;
|
$this->middle = $table;
|
||||||
$this->alias = $alias;
|
|
||||||
$this->query = (new $model)->db();
|
$this->query = (new $model)->db();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,15 +25,13 @@ class HasMany extends Relation
|
|||||||
* @param string $model 模型名
|
* @param string $model 模型名
|
||||||
* @param string $foreignKey 关联外键
|
* @param string $foreignKey 关联外键
|
||||||
* @param string $localKey 关联主键
|
* @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->parent = $parent;
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
$this->foreignKey = $foreignKey;
|
$this->foreignKey = $foreignKey;
|
||||||
$this->localKey = $localKey;
|
$this->localKey = $localKey;
|
||||||
$this->alias = $alias;
|
|
||||||
$this->query = (new $model)->db();
|
$this->query = (new $model)->db();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,9 +33,8 @@ class HasManyThrough extends Relation
|
|||||||
* @param string $firstkey 关联外键
|
* @param string $firstkey 关联外键
|
||||||
* @param string $secondKey 关联外键
|
* @param string $secondKey 关联外键
|
||||||
* @param string $localKey 关联主键
|
* @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->parent = $parent;
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
@@ -43,7 +42,6 @@ class HasManyThrough extends Relation
|
|||||||
$this->foreignKey = $foreignKey;
|
$this->foreignKey = $foreignKey;
|
||||||
$this->throughKey = $throughKey;
|
$this->throughKey = $throughKey;
|
||||||
$this->localKey = $localKey;
|
$this->localKey = $localKey;
|
||||||
$this->alias = $alias;
|
|
||||||
$this->query = (new $model)->db();
|
$this->query = (new $model)->db();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,16 +22,14 @@ class HasOne extends OneToOne
|
|||||||
* @param string $model 模型名
|
* @param string $model 模型名
|
||||||
* @param string $foreignKey 关联外键
|
* @param string $foreignKey 关联外键
|
||||||
* @param string $localKey 关联主键
|
* @param string $localKey 关联主键
|
||||||
* @param array $alias 别名定义
|
|
||||||
* @param string $joinType JOIN类型
|
* @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->parent = $parent;
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
$this->foreignKey = $foreignKey;
|
$this->foreignKey = $foreignKey;
|
||||||
$this->localKey = $localKey;
|
$this->localKey = $localKey;
|
||||||
$this->alias = $alias;
|
|
||||||
$this->joinType = $joinType;
|
$this->joinType = $joinType;
|
||||||
$this->query = (new $model)->db();
|
$this->query = (new $model)->db();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ class MorphTo extends Relation
|
|||||||
// 多态字段
|
// 多态字段
|
||||||
protected $morphKey;
|
protected $morphKey;
|
||||||
protected $morphType;
|
protected $morphType;
|
||||||
|
// 多态别名
|
||||||
|
protected $alias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 架构函数
|
* 架构函数
|
||||||
@@ -74,6 +76,18 @@ class MorphTo extends Relation
|
|||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置多态别名
|
||||||
|
* @access public
|
||||||
|
* @param array $alias 别名定义
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setAlias($alias)
|
||||||
|
{
|
||||||
|
$this->alias = $alias;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预载入关联查询
|
* 预载入关联查询
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ abstract class OneToOne extends Relation
|
|||||||
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
|
public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
|
||||||
{
|
{
|
||||||
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
|
$name = Loader::parseName(basename(str_replace('\\', '/', $query->getModel())));
|
||||||
$alias = isset($this->alias[$name]) ? $this->alias[$name] : $name;
|
$alias = $name;
|
||||||
if ($first) {
|
if ($first) {
|
||||||
$table = $query->getTable();
|
$table = $query->getTable();
|
||||||
$query->table([$table => $alias]);
|
$query->table([$table => $alias]);
|
||||||
@@ -53,7 +53,7 @@ abstract class OneToOne extends Relation
|
|||||||
// 预载入封装
|
// 预载入封装
|
||||||
$joinTable = $this->query->getTable();
|
$joinTable = $this->query->getTable();
|
||||||
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
|
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
|
||||||
$joinAlias = isset($this->alias[$joinName]) ? $this->alias[$joinName] : $relation;
|
$joinAlias = $relation;
|
||||||
$query->via($joinAlias);
|
$query->via($joinAlias);
|
||||||
|
|
||||||
if ($this instanceof BelongsTo) {
|
if ($this instanceof BelongsTo) {
|
||||||
|
|||||||
Reference in New Issue
Block a user