mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
morphTo支持多态类型字段别名定义
This commit is contained in:
@@ -1388,10 +1388,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
/**
|
/**
|
||||||
* MORPH TO 关联定义
|
* MORPH TO 关联定义
|
||||||
* @access public
|
* @access public
|
||||||
* @param string|array $morph 多态字段信息
|
* @param string|array $morph 多态字段信息
|
||||||
|
* @param array $alias 多态别名定义
|
||||||
* @return Relation
|
* @return Relation
|
||||||
*/
|
*/
|
||||||
public function morphTo($morph)
|
public function morphTo($morph, $alias = [])
|
||||||
{
|
{
|
||||||
// 记录当前关联信息
|
// 记录当前关联信息
|
||||||
if (is_array($morph)) {
|
if (is_array($morph)) {
|
||||||
@@ -1400,7 +1401,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$morphType = $morph . '_type';
|
$morphType = $morph . '_type';
|
||||||
$foreignKey = $morph . '_id';
|
$foreignKey = $morph . '_id';
|
||||||
}
|
}
|
||||||
return $this->relation()->morphTo($morphType, $foreignKey);
|
return $this->relation()->morphTo($morphType, $foreignKey, $alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __call($method, $args)
|
public function __call($method, $args)
|
||||||
|
|||||||
@@ -129,10 +129,16 @@ class Relation
|
|||||||
case self::MORPH_TO:
|
case self::MORPH_TO:
|
||||||
// 多态模型
|
// 多态模型
|
||||||
$model = $this->parent->{$this->middle};
|
$model = $this->parent->{$this->middle};
|
||||||
$path = explode('\\', get_class($this->parent));
|
// 多态类型映射
|
||||||
array_pop($path);
|
if (isset($this->alias[$model])) {
|
||||||
array_push($path, Loader::parseName($model, 1));
|
$model = $this->alias[$model];
|
||||||
$model = implode('\\', $path);
|
}
|
||||||
|
if (false === strpos($model, '\\')) {
|
||||||
|
$path = explode('\\', get_class($this->parent));
|
||||||
|
array_pop($path);
|
||||||
|
array_push($path, Loader::parseName($model, 1));
|
||||||
|
$model = implode('\\', $path);
|
||||||
|
}
|
||||||
// 主键数据
|
// 主键数据
|
||||||
$pk = $this->parent->{$this->foreignKey};
|
$pk = $this->parent->{$this->foreignKey};
|
||||||
$result = (new $model)->find($pk);
|
$result = (new $model)->find($pk);
|
||||||
@@ -559,14 +565,16 @@ class Relation
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $morphType 多态字段名
|
* @param string $morphType 多态字段名
|
||||||
* @param string $foreignKey 外键名
|
* @param string $foreignKey 外键名
|
||||||
|
* @param array $alias 多态别名定义
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function morphTo($morphType, $foreignKey)
|
public function morphTo($morphType, $foreignKey, $alias)
|
||||||
{
|
{
|
||||||
// 记录当前关联信息
|
// 记录当前关联信息
|
||||||
$this->type = self::MORPH_TO;
|
$this->type = self::MORPH_TO;
|
||||||
$this->middle = $morphType;
|
$this->middle = $morphType;
|
||||||
$this->foreignKey = $foreignKey;
|
$this->foreignKey = $foreignKey;
|
||||||
|
$this->alias = $alias;
|
||||||
// 返回关联的模型对象
|
// 返回关联的模型对象
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user