Skip to content

Commit

Permalink
Add database utils and first method to delete entities
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest committed Jun 3, 2024
1 parent 1983de3 commit e2213e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Basecom\FixturePlugin\Utils\CategoryUtils;
use Basecom\FixturePlugin\Utils\CmsUtils;
use Basecom\FixturePlugin\Utils\CustomerUtils;
use Basecom\FixturePlugin\Utils\DatabaseUtils;
use Basecom\FixturePlugin\Utils\MediaUtils;
use Basecom\FixturePlugin\Utils\PaymentMethodUtils;
use Basecom\FixturePlugin\Utils\SalesChannelUtils;
Expand All @@ -22,6 +23,7 @@ public function __construct(
private PaymentMethodUtils $paymentMethodUtils,
private ShippingMethodUtils $shippingMethodUtils,
private CustomerUtils $customerUtils,
private DatabaseUtils $databaseUtils,
) {
}

Expand Down Expand Up @@ -87,4 +89,13 @@ public function ShippingMethod(): ShippingMethodUtils
{
return $this->shippingMethodUtils;
}

/**
* Use this to access the general database helper functions
* of the fixture helper class.
*/
public function Database(): DatabaseUtils
{
return $this->databaseUtils;
}
}
31 changes: 31 additions & 0 deletions src/Utils/DatabaseUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;

readonly class DatabaseUtils
{
public function __construct(
private DefinitionInstanceRegistry $definitionInstanceRegistry,
) {
}

public function deleteEntities(string $entity, Criteria $criteria): void
{
$repository = $this->definitionInstanceRegistry->getRepository($entity);

// First load all the ids of the entities
$ids = $repository->searchIds($criteria, Context::createDefaultContext())->getData();

// Delete all entities with the IDs
$repository->delete(
array_values($ids),
Context::createDefaultContext(),
);
}
}

0 comments on commit e2213e2

Please sign in to comment.