From edbbad9a01cbba0680d6a23a43e6503586dc72b5 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 18 Jan 2024 13:22:39 +0000 Subject: [PATCH] Add `setTags` > Drupal\Core\Entity\EntityStorageException: SQLSTATE[HY000]: General error: 1 no such table: t est93789242.taxonomy_term_data: INSERT INTO "test93789242"."taxonomy_term_data" ("revision_id", "v id", "uuid", "langcode") VALUES (?, ?, ?, ?); Array --- .../example/src/Builder/PostBuilder.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/web/modules/custom/example/src/Builder/PostBuilder.php b/web/modules/custom/example/src/Builder/PostBuilder.php index 1ed5ac4..cf00c6e 100644 --- a/web/modules/custom/example/src/Builder/PostBuilder.php +++ b/web/modules/custom/example/src/Builder/PostBuilder.php @@ -14,6 +14,11 @@ final class PostBuilder { private bool $isPublished = TRUE; + /** + * @var string[] + */ + private array $tags = []; + public static function create(): self { return new self(); } @@ -36,6 +41,15 @@ final class PostBuilder { return $this; } + /** + * @param string[] $tags + */ + public function setTags(array $tags): self { + $this->tags = $tags; + + return $this; + } + public function setTitle(string $title): self { $this->title = $title; @@ -53,6 +67,19 @@ final class PostBuilder { $post->setCreatedTime($this->created->getTimestamp()); } + $tagTerms = []; + if ($this->tags !== []) { + foreach ($this->tags as $tag) { + $term = Term::create([ + 'name' => $tag, + 'vid' => 'tags', + ]); + + $term->save(); + + $tagTerms[] = $term; + } + } $post->save(); return $post;