diff --git a/src/Entity/TagMap.php b/src/Entity/TagMap.php index 0836a5e8..990a3a9c 100644 --- a/src/Entity/TagMap.php +++ b/src/Entity/TagMap.php @@ -11,6 +11,8 @@ use Windwalker\ORM\EntityTrait; use Windwalker\ORM\Metadata\EntityMetadata; +use function Windwalker\unwrap_enum; + /** * The TagMap class. */ @@ -64,9 +66,9 @@ public function getType(): string return $this->type; } - public function setType(string $type): static + public function setType(string|\BackedEnum $type): static { - $this->type = $type; + $this->type = (string) unwrap_enum($type); return $this; } diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 5a8ea498..6d85e4f1 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -10,6 +10,7 @@ use Windwalker\Database\Driver\StatementInterface; use Windwalker\ORM\EntityMapper; use Windwalker\ORM\ORM; +use Windwalker\ORM\SelectorQuery; use Windwalker\Utilities\Str; /** @@ -51,17 +52,15 @@ public function createTagsIfNew(iterable $tagIds): array } /** - * flushTags - * - * @param string $type - * @param mixed $targetId - * @param iterable $tagIds + * @param string|\BackedEnum $type + * @param mixed $targetId + * @param iterable $tagIds * * @return iterable * * @throws ReflectionException */ - public function flushTagMapsFromInput(string $type, mixed $targetId, iterable $tagIds): iterable + public function flushTagMapsFromInput(string|\BackedEnum $type, mixed $targetId, iterable $tagIds): iterable { $tagIds = $this->createTagsIfNew($tagIds); @@ -69,17 +68,15 @@ public function flushTagMapsFromInput(string $type, mixed $targetId, iterable $t } /** - * flushTags - * - * @param string $type - * @param mixed $targetId - * @param iterable $tagIds + * @param string|\BackedEnum $type + * @param mixed $targetId + * @param iterable $tagIds * * @return iterable * * @throws ReflectionException */ - public function flushTagMaps(string $type, mixed $targetId, iterable $tagIds): iterable + public function flushTagMaps(string|\BackedEnum $type, mixed $targetId, iterable $tagIds): iterable { /** @var EntityMapper $tagMapMapper */ $tagMapMapper = $this->orm->mapper(TagMap::class); @@ -101,16 +98,16 @@ public function flushTagMaps(string $type, mixed $targetId, iterable $tagIds): i * @param string $type * @param mixed $targetId * - * @return array + * @return void * * @throws ReflectionException */ - public function clearMapsOfTarget(string $type, mixed $targetId): array + public function clearMapsOfTarget(string $type, mixed $targetId): void { /** @var EntityMapper $tagMapMapper */ $tagMapMapper = $this->orm->mapper(TagMap::class); - return $tagMapMapper->deleteWhere( + $tagMapMapper->deleteWhere( [ 'type' => $type, 'target_id' => $targetId, @@ -118,12 +115,12 @@ public function clearMapsOfTarget(string $type, mixed $targetId): array ); } - public function clearMapsOfTag(string $type, mixed $tagId): array + public function clearMapsOfTag(string $type, mixed $tagId): void { /** @var EntityMapper $tagMapMapper */ $tagMapMapper = $this->orm->mapper(TagMap::class); - return $tagMapMapper->deleteWhere( + $tagMapMapper->deleteWhere( [ 'type' => $type, 'tag_id' => $tagId, @@ -131,6 +128,24 @@ public function clearMapsOfTag(string $type, mixed $tagId): array ); } + public function getTagListQuery(string|\BackedEnum $type, mixed $targetIds = null): SelectorQuery + { + $query = $this->orm->from(Tag::class) + ->leftJoin(TagMap::class) + ->where('tag_map.type', $type); + + if (is_iterable($targetIds)) { + $targetIds = iterator_to_array($targetIds); + } + + if ($targetIds !== null && $targetIds !== []) { + $query->where('tag_map.target_id', $targetIds); + } + + return $query->groupByJoins() + ->setDefaultItemClass(Tag::class); + } + /** * @return string */