To control the order in which objects are actually persisted to the database, you can decorate the fidry_alice_data_fixtures.loader.simple service by implementing a custom LoaderInterface.
Implementation Steps:
- Create a class implementing
LoaderInterface that decorates the existing loader. - In the
load method, call the decorated loader to get the $objects array. - Re-order the
$objects array as needed. - Register the class in
services.yaml using the decorates attribute.
Example:
final class CustomOrderLoader implements LoaderInterface
{
use IsAServiceTrait;
private $decoratedLoader;
public function __construct(LoaderInterface $decoratedLoader)
{
$this->decoratedLoader = $decoratedLoader;
}
public function load(array $fixturesFiles, array $parameters = [], array $objects = [], PurgeMode $purgeMode = null): array
{
$objects = $this->decoratedLoader->load($fixturesFiles, $parameters, $objects, $purgeMode);
// TODO: re-order $objects
return $objects;
}
}
// services.yaml
services:
Acme\Alice\Loader\CustomOrderLoader:
decorates: 'fidry_alice_data_fixtures.loader.simple'
arguments:
- '@Acme\Alice\Loader\CustomOrderLoader.inner'
services:
Acme\Alice\Loader\CustomOrderLoader:
decorates: 'fidry_alice_data_fixtures.loader.simple'
arguments:
- '@Acme\Alice\Loader\CustomOrderLoader.inner'