Skip to content

2.0.0-beta.1

Pre-release
Pre-release

Choose a tag to compare

@dereuromark dereuromark released this 06 May 01:57

Highlights

  • immutable v2 factory API with new(), from(), count(), build(), buildMany(), save(), saveMany()
  • query/read split via query() and direct table access via table()
  • directional association API with for() / has() and updated bake output
  • new ergonomics: state(), sequence(), afterBuild(), afterSave()
  • bundled Rector migration support for safe mechanical upgrades
  • v2 docs and upgrade guides aligned with the new API surface

Migration notes

  • Migrate first to latest 1.x (via scripts), then jump to 2.x
  • deprecated 1.x wrappers have been removed from the v2 surface
  • use the bundled rector.php config for mechanical call-site updates
  • Factory::get(...) is replaced by Factory::table()->get(...) (normal Table class usage)

API at a glance

// Singular, in-memory
$user = UserFactory::new()->build();

// Singular, persisted
$user = UserFactory::new()->save();

// Singular, with overrides at construction
$user = UserFactory::new(['name' => 'Foo'])->save();

// Singular, last-mile override at terminal
$user = UserFactory::new()->save(['name' => 'Foo']);

// Singular, with ad hoc state layered in the chain
$user = UserFactory::new()
    ->state(['name' => 'Foo'])
    ->save();

// Singular, with reusable named states
$user = UserFactory::new()
    ->admin()
    ->verified()
    ->save();

// Plural via count() modifier — note the explicit *Many() terminal
$users = UserFactory::new()->count(5)->saveMany();
$users = UserFactory::new()->count(5)->buildMany();

// Plural with overrides applied to all
$users = UserFactory::new(['admin' => true])->count(3)->saveMany();

// Plural with per-row variation via sequence()
$users = UserFactory::new()
    ->count(3)
    ->sequence(
        ['role' => 'admin'],
        ['role' => 'editor'],
        ['role' => 'user'],
    )
    ->saveMany();

// Lifecycle hooks
$user = UserFactory::new()
    ->afterBuild(fn (User $user) => $user->name = 'Built name')
    ->afterSave(fn (User $user) => $user->synced = true)
    ->save();

// Wrap an existing entity
$user = UserFactory::from($existingEntity)->save();