Skip to content

Commit

Permalink
增加 多态关系的模型名映射别名的数组$morphMap
Browse files Browse the repository at this point in the history
增加 多态关系的模型名映射别名的数组$morphMap
  • Loading branch information
OHOM authored and liu21st committed Jul 5, 2022
1 parent 140619a commit 97b061b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/model/relation/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
class MorphToMany extends BelongsToMany
{

/**
* 多态关系的模型名映射别名的数组
*
* @var array
*/
protected static $morphMap = [];

/**
* 多态字段名
* @var string
Expand Down Expand Up @@ -58,6 +65,9 @@ public function __construct(Model $parent, string $model, string $middle, string
$this->morphType = $morphType;
$this->inverse = $inverse;
$this->morphClass = $inverse ? $model : get_class($parent);
if (isset(static::$morphMap[$this->morphClass])) {
$this->morphClass = static::$morphMap[$this->morphClass];
}

$foreignKey = $inverse ? $morphKey : $localKey;
$localKey = $inverse ? $localKey : $morphKey;
Expand Down Expand Up @@ -460,4 +470,21 @@ protected function baseQuery(): void
}
}

/**
* 设置或获取多态关系的模型名映射别名的数组
*
* @param array|null $map
* @param bool $merge
* @return array
*/
public static function morphMap(array $map = null, $merge = true): array
{
if (is_array($map)) {
static::$morphMap = $merge && static::$morphMap
? $map + static::$morphMap : $map;
}

return static::$morphMap;
}

}

0 comments on commit 97b061b

Please sign in to comment.