## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require --dev theofidry/alice-data-fixtures
For Symfony Flex projects, run:
composer require --dev theofidry/alice-data-fixtures
php bin/console make:fixtures
First Fixture File (database/fixtures/users.yml):
App\Models\User:
user1:
name: 'John Doe'
email: 'john@example.com'
password: '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
Load Fixtures:
php bin/console fixtures:load
For Eloquent (non-Symfony):
php artisan db:seed --class=YourFixtureSeeder
Verify:
php bin/console doctrine:fixtures:load --append
Or check your database directly.
posts.yml fixture with relationships to pre-populated users.yml:
App\Models\Post:
post1:
title: 'First Post'
content: 'Hello World'
user: '@user1' # Reference to user1 from users.yml
Define Fixtures:
database/fixtures/ (Symfony) or database/fixtures/ (Laravel).@user1) for relationships.Load Fixtures:
php bin/console fixtures:loadphp artisan db:seed --class=YourSeeder--append to add to existing data (avoids truncation).Organize Fixtures:
users.yml, posts.yml, roles.yml).tags: [users, admin]) to load subsets:
php bin/console fixtures:load --tag=users
Integration with Tests:
setUp() for PHPUnit:
public function setUp(): void {
$this->loadFixtures(['users.yml', 'posts.yml']);
}
Complex Relationships:
App\Models\Category:
electronics:
name: 'Electronics'
subcategories:
- name: 'Phones'
- name: 'Laptops'
Dynamic Data:
faker expressions in fixtures:
App\Models\User:
user{1..10}:
name: '<firstName> <lastName>'
email: '<email>'
Custom Factories:
// src/FixtureFactory.php
public function createUserWithRole(string $roleName) {
return $this->procedure('App\Models\User', [
'name' => 'Admin',
'email' => 'admin@example.com',
'roles' => [$roleName],
]);
}
Seeding in Migrations:
// database/migrations/...
public function up() {
Schema::create('users');
$this->loadFixtures(['users.yml']);
}
Symfony:
config/bundles.php (if not using Flex).config/packages/nelmio_alice.yaml:
nelmio_alice:
fixtures:
paths: ['%kernel.project_dir%/database/fixtures']
loaded_fixtures: 'database/fixtures/loaded_fixtures.yml'
Laravel:
$this->app->bind('fixtures.loader', function ($app) {
return new \Theofidry\AliceDataFixtures\Loader\Loader(
$app['path.database'].'/fixtures',
$app['path.database'].'/fixtures/loaded_fixtures.yml'
);
});
Doctrine Events:
postInstall):
# config/packages/doctrine.yaml
doctrine:
dbal:
connections:
default:
events:
postInstall: ['fixtures:load']
CI/CD:
# .github/workflows/tests.yml
- run: php bin/console fixtures:load --env=test
Duplicate Data:
# config/packages/nelmio_alice.yaml
nelmio_alice:
persistence:
orm:
truncate: false # Allow duplicates
Transaction Failures:
--no-transaction:
php bin/console fixtures:load --no-transaction
Circular References:
User referencing Post which references User). Use lazy loading or break cycles:
App\Models\User:
user1:
posts: ['@post1', '@post2'] # Ensure posts exist first
Eloquent vs. Doctrine:
// config/app.php
'providers' => [
Illuminate\Database\Eloquent\Model::class,
],
Dry Run:
php bin/console fixtures:load --dry-run
Verbose Output:
php bin/console fixtures:load -vvv
Loaded Fixtures File:
database/fixtures/loaded_fixtures.yml to verify what was loaded.Doctrine Events:
php bin/console debug:event-dispatcher
Partial Loading:
php bin/console fixtures:load --fixtures=users.yml,posts.yml
Environment-Specific Fixtures:
# config/packages/nelmio_alice.yaml
nelmio_alice:
fixtures:
paths:
'%kernel.environment%': '%kernel.project_dir%/database/fixtures/%kernel.environment%'
Custom Loaders:
class CustomLoader extends \Theofidry\AliceDataFixtures\Loader\Loader {
public function load(array $fixtures) {
// Pre-process fixtures here
parent::load($fixtures);
}
}
Performance:
# config/packages/nelmio_alice.yaml
nelmio_alice:
persistence:
orm:
enabled: false
Testing:
public function tearDown(): void {
$this->loadFixtures(['--purge' => true]);
}
PHPCR/ODM:
nelmio_alice:
persistence:
odm:
enabled: true
Seeding in Production:
if (app()->environment('production')) {
throw new \RuntimeException('Fixtures cannot be loaded in production.');
}
Custom References:
$loader->setReferenceResolver(new CustomReferenceResolver());
Fixture Validation:
php bin/console debug:container --parameter=nelmio_alice.fixtures.paths
**Backup Before
How can I help you explore Laravel packages today?