Skip to content

Commit a8a79a1

Browse files
Add flush to taxon-adapter (#112)
1 parent 429f08c commit a8a79a1

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

Adapter/TaxonCategoryAdapter.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Sulu\Bundle\SyliusConsumerBundle\Adapter;
1515

16+
use Doctrine\ORM\EntityManagerInterface;
1617
use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface;
1718
use Sulu\Bundle\CategoryBundle\Entity\CategoryRepositoryInterface;
1819
use Sulu\Bundle\CategoryBundle\Entity\CategoryTranslationInterface;
@@ -38,19 +39,30 @@ class TaxonCategoryAdapter implements TaxonAdapterInterface
3839
*/
3940
private $categoryTranslationRepository;
4041

42+
/**
43+
* @var EntityManagerInterface
44+
*/
45+
private $entityManager;
46+
4147
public function __construct(
4248
TaxonCategoryBridgeRepositoryInterface $taxonCategoryBridgeRepository,
4349
CategoryRepositoryInterface $categoryRepository,
44-
CategoryTranslationRepositoryInterface $categoryTranslationRepository
50+
CategoryTranslationRepositoryInterface $categoryTranslationRepository,
51+
EntityManagerInterface $entityManager
4552
) {
4653
$this->taxonCategoryBridgeRepository = $taxonCategoryBridgeRepository;
4754
$this->categoryRepository = $categoryRepository;
4855
$this->categoryTranslationRepository = $categoryTranslationRepository;
56+
$this->entityManager = $entityManager;
4957
}
5058

5159
public function synchronize(TaxonPayload $payload): void
5260
{
5361
$this->handlePayload($payload);
62+
63+
// Needed to use categories in other adapters
64+
// (e.g. category pages with a smart-content filtered by the sylius category)
65+
$this->entityManager->flush();
5466
}
5567

5668
private function handlePayload(TaxonPayload $payload, ?CategoryInterface $parent = null): void

Payload/ProductPayload.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function isEnabled(): bool
4444
return $this->payload->getBoolValue('enabled');
4545
}
4646

47-
public function getMainTaxonId(): int
47+
public function getMainTaxonId(): ?int
4848
{
49-
return $this->payload->getIntValue('mainTaxonId');
49+
return $this->payload->getNullableIntValue('mainTaxonId');
5050
}
5151

5252
/**

Resources/config/taxon_category_adapter.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<argument type="service" id="sulu.repository.taxon_category_bridge"/>
1010
<argument type="service" id="sulu.repository.category"/>
1111
<argument type="service" id="sulu.repository.category_translation"/>
12+
<argument type="service" id="doctrine.orm.entity_manager"/>
1213

1314
<tag name="sulu_sylius_consumer.adapter.taxon"/>
1415
</service>

Tests/Application/config/config.yml

-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
doctrine:
33
dbal:
44
url: '%env(resolve:DATABASE_URL)%'
5-
orm:
6-
mappings:
7-
gedmo_tree:
8-
type: xml
9-
prefix: Gedmo\Tree\Entity
10-
dir: "%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
11-
alias: GedmoTree
12-
is_bundle: false
135

146
framework:
157
router:

Tests/Functional/Adapter/TaxonCategoryAdapterTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function testSynchronize(): void
3838
$taxonPayload = new TaxonPayload(1, MockSyliusData::TAXON);
3939

4040
$adapter->synchronize($taxonPayload);
41-
$this->getEntityManager()->flush();
41+
42+
// Adapter flushed the entity-manager - by clearing it we can check if that works correctly
43+
$this->getEntityManager()->clear();
4244

4345
$bridge1 = $this->getEntityManager()->find(TaxonCategoryBridge::class, 1);
4446
$bridge2 = $this->getEntityManager()->find(TaxonCategoryBridge::class, 2);

0 commit comments

Comments
 (0)