Skip to content

Commit fa33098

Browse files
committed
fix: add term to an entity which already have it won't throw exception anymore
1 parent 46a0e9c commit fa33098

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

src/Bridge/Repository/TermRepository.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Williarin\WordpressInterop\Bridge\Repository;
66

7+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
78
use Doctrine\DBAL\Query\QueryBuilder;
89
use Williarin\WordpressInterop\Bridge\Entity\BaseEntity;
910
use Williarin\WordpressInterop\Bridge\Entity\Term;
@@ -62,17 +63,20 @@ public function addTermsToEntity(BaseEntity $entity, array $terms): void
6263
continue;
6364
}
6465

65-
$this->entityManager->getConnection()
66-
->createQueryBuilder()
67-
->insert($this->entityManager->getTablesPrefix() . 'term_relationships')
68-
->values([
69-
'object_id' => '?',
70-
'term_taxonomy_id' => '?',
71-
'term_order' => '0',
72-
])
73-
->setParameters([$entity->id, (int) $term->termTaxonomyId])
74-
->executeStatement()
75-
;
66+
try {
67+
$this->entityManager->getConnection()
68+
->createQueryBuilder()
69+
->insert($this->entityManager->getTablesPrefix() . 'term_relationships')
70+
->values([
71+
'object_id' => '?',
72+
'term_taxonomy_id' => '?',
73+
'term_order' => '0',
74+
])
75+
->setParameters([$entity->id, (int) $term->termTaxonomyId])
76+
->executeStatement()
77+
;
78+
} catch (UniqueConstraintViolationException) {
79+
}
7680
}
7781

7882
$this->recountTerms();

test/Test/Bridge/Repository/TermRepositoryTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,39 @@ public function testAddTermsToEntity(): void
133133
self::assertEquals([1, 6, 1, 2], array_column($termsProduct, 'count'));
134134
}
135135

136+
public function testAddDuplicateTermsToEntityDoesNotDuplicateThem(): void
137+
{
138+
$hoodieTerms = $this->repository->findBy([
139+
new PostRelationshipCondition(Product::class, [
140+
'post_status' => new Operand(['publish', 'private'], Operand::OPERATOR_IN),
141+
'sku' => 'super-forces-hoodie',
142+
]),
143+
]);
144+
145+
$product = $this->manager->getRepository(Product::class)
146+
->find(16)
147+
;
148+
149+
$termsProduct = $this->repository->findBy([
150+
new PostRelationshipCondition(Product::class, [
151+
'id' => $product->id,
152+
]),
153+
]);
154+
155+
self::assertEquals(['simple', 'Hoodies'], array_column($termsProduct, 'name'));
156+
157+
$this->repository->addTermsToEntity($product, $hoodieTerms);
158+
159+
$termsProduct = $this->repository->findBy([
160+
new PostRelationshipCondition(Product::class, [
161+
'id' => $product->id,
162+
]),
163+
]);
164+
165+
self::assertEquals(['simple', 'Hoodies', 'MegaBrand'], array_column($termsProduct, 'name'));
166+
self::assertEquals([15, 5, 2], array_column($termsProduct, 'count'));
167+
}
168+
136169
public function testAddTermsToEntityWithoutTermTaxonomyIdAreIgnored(): void
137170
{
138171
$hoodieTerms = array_map(

0 commit comments

Comments
 (0)