add tag assign to documents batch
This commit is contained in:
@@ -110,6 +110,49 @@ final class TagService
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// TAG → DOCUMENT SYNC (Bulk Assign)
|
||||
// =========================================================
|
||||
|
||||
/**
|
||||
* Synchronisiert alle Dokumente eines Tags.
|
||||
* Löst einen Rebuild aus, da document_ids Teil des NDJSON sind.
|
||||
*/
|
||||
public function syncTagDocuments(Tag $tag, array $newDocumentIds): void
|
||||
{
|
||||
$newDocumentIds = array_unique($newDocumentIds);
|
||||
|
||||
$currentRelations = $this->em
|
||||
->getRepository(DocumentTag::class)
|
||||
->findBy(['tag' => $tag]);
|
||||
|
||||
$currentDocumentIds = array_map(
|
||||
fn(DocumentTag $dt) => (string) $dt->getDocument()->getId(),
|
||||
$currentRelations
|
||||
);
|
||||
|
||||
$toAdd = array_diff($newDocumentIds, $currentDocumentIds);
|
||||
$toRemove = array_diff($currentDocumentIds, $newDocumentIds);
|
||||
|
||||
foreach ($toAdd as $documentId) {
|
||||
$document = $this->em->getRepository(Document::class)->find($documentId);
|
||||
if ($document instanceof Document) {
|
||||
$this->em->persist(new DocumentTag($document, $tag));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($currentRelations as $relation) {
|
||||
if (in_array((string) $relation->getDocument()->getId(), $toRemove, true)) {
|
||||
$this->em->remove($relation);
|
||||
}
|
||||
}
|
||||
|
||||
if ($toAdd || $toRemove) {
|
||||
$this->em->flush();
|
||||
$this->triggerRebuildIfIdle();
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// INTERNAL HELPERS
|
||||
// =========================================================
|
||||
|
||||
Reference in New Issue
Block a user