Installation
Add the bundle to your composer.json:
composer require cekurte/fixturesbundle
Enable it in config/bundles.php:
return [
// ...
Cekurte\FixturesBundle\CekurteFixturesBundle::class => ['all' => true],
];
Basic Configuration
Ensure doctrine:fixtures:load is available by running:
php bin/console doctrine:fixtures:load
(Requires DoctrineFixturesBundle; install via composer require orm-fixtures if missing.)
First Fixture File
Create a YAML fixture file (e.g., src/DataFixtures/ORM/LoadUserData.yml):
App\Entity\User:
user1:
email: 'test@example.com'
password: '$2y$10$...' # Use a hashed password
roles: ['ROLE_USER']
Load Fixtures Run:
php bin/console doctrine:fixtures:load
Verify data in the database.
src/DataFixtures/ORM/ for template files.config/packages/cekurte_fixtures.yaml (if created).Use the bundle to populate a database with default records (e.g., users, roles, or test data) during development or testing. Example:
# src/DataFixtures/ORM/LoadDefaultRoles.yml
App\Entity\Role:
admin:
name: 'ROLE_ADMIN'
description: 'Administrator role'
user:
name: 'ROLE_USER'
description: 'Regular user role'
Development Workflow
src/DataFixtures/ORM/ (e.g., LoadUsers.yml, LoadProducts.yml).App\Entity\Order:
order1:
user: '@user1' # Reference to user1 from LoadUsers.yml
total: 99.99
php bin/console doctrine:fixtures:load --append # Append to existing data
Testing Workflow
--purge-with-truncate to reset the database before tests:
php bin/console doctrine:fixtures:load --purge-with-truncate
// phpunit.xml
<env name="DATABASE_URL" value="sqlite:///:memory:" />
Environment-Specific Fixtures
dev/, test/ directories).config/packages/cekurte_fixtures.yaml:
cekurte_fixtures:
directories: ['%kernel.project_dir%/config/fixtures/%env(APP_ENV)%']
Custom Fixture Classes
Extend Cekurte\FixturesBundle\Fixtures\AbstractFixture for dynamic data:
namespace App\DataFixtures\ORM;
use Cekurte\FixturesBundle\Fixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
class LoadDynamicUsers extends AbstractFixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager)
{
$users = ['alice@example.com', 'bob@example.com'];
foreach ($users as $email) {
$user = new \App\Entity\User();
$user->setEmail($email);
$manager->persist($user);
}
$manager->flush();
}
public function getDependencies()
{
return [LoadDefaultRoles::class];
}
}
Dependencies Between Fixtures
Use DependentFixtureInterface to enforce load order:
# LoadUsers.yml
App\Entity\User:
user1:
email: 'test@example.com'
# LoadOrders.yml
App\Entity\Order:
order1:
user: '@user1' # Requires LoadUsers.yml to run first
Parameterized Fixtures
Use Symfony parameters (e.g., APP_ENV) to conditionally load data:
# config/packages/cekurte_fixtures.yaml
cekurte_fixtures:
enabled: '%env(bool:APP_LOAD_FIXTURES)%'
# LoadDevOnlyData.yml
App\Entity\DevOnlyEntity:
dev_data:
value: 'Only loaded in dev'
Missing DoctrineFixturesBundle
Command "doctrine:fixtures:load" not found.orm-fixtures:
composer require orm-fixtures
Circular References
User references Order, which references User).setReference() in custom fixtures or restructure data.Database State Conflicts
--append or --purge-with-truncate:
php bin/console doctrine:fixtures:load --append
Locale/Encoding Issues
Enable Debug Mode
Set APP_DEBUG=1 in .env to see detailed fixture load logs.
Check Load Order
Use getDependencies() in custom fixtures to enforce order:
public function getDependencies()
{
return [LoadRoles::class, LoadUsers::class];
}
Validate Fixtures
Run with --dry-run to preview changes:
php bin/console doctrine:fixtures:load --dry-run
Clear Cache After modifying fixtures or bundle config, clear the cache:
php bin/console cache:clear
Custom Fixture Directories
Override the default src/DataFixtures/ORM/ path in config:
cekurte_fixtures:
directories: ['%kernel.project_dir%/data/fixtures']
Excluding Fixtures
Use a ~ prefix in fixture filenames to exclude them (e.g., LoadDisabledData~.yml).
Environment-Specific Config
Load different fixtures per environment via config/packages/cekurte_fixtures_{env}.yaml.
Custom Fixture Loader
Extend Cekurte\FixturesBundle\Loader\FixturesLoader to add pre/post-load logic:
namespace App\Fixtures;
use Cekurte\FixturesBundle\Loader\FixturesLoader;
class CustomFixturesLoader extends FixturesLoader
{
protected function preLoad()
{
// Custom logic before loading
}
protected function postLoad()
{
// Custom logic after loading
}
}
Register in config/services.yaml:
services:
App\Fixtures\CustomFixturesLoader:
decorates: 'cekurte_fixtures.loader'
arguments: ['@.inner']
Event Listeners
Subscribe to fixtures.load events for dynamic behavior:
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Cekurte\FixturesBundle\Event\FixturesEvent;
class FixtureSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'fixtures.load' => 'onFixturesLoad',
];
}
public function onFixturesLoad(FixturesEvent $event)
{
// Modify fixtures dynamically
}
}
Custom Data Transformers
Override how data is loaded by extending Cekurte\FixturesBundle\Transformer\DataTransformer.
How can I help you explore Laravel packages today?