Download the PHP package stemble/laravel-doctrine-factory without Composer

On this page you can find all versions of the php package stemble/laravel-doctrine-factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-doctrine-factory

Laravel Doctrine Factory

Tests

Use Eloquent Factories with your Doctrine Entities.

Why Not the Official Factories?

laravel-doctrine/orm ships with its own factory system, but its API is frozen at Laravel's pre-8.x style:

By contrast, this package keeps you on Laravel's modern factory API. If you already know Eloquent factories you already know this package — read the Laravel docs and use this package as a drop-in replacement. See Feature Compatibility for the full status grid.

Installation

Install via Composer:

Getting Started

Add Laravel's HasFactory trait to your entity and annotate it with @implements HasFactory<YourFactory>. The annotation is optional but gives IDEs full type inference — User::factory() returns UserFactory, so state methods like ->admin() are recognised without a cast.

DoctrineFactory extends Laravel's Illuminate\Database\Eloquent\Factories\Factory rather than replacing it, so the standard HasFactory trait works without modification — there is no Doctrine-specific version to import.

Then create a factory that extends Stemble\LaravelDoctrineFactory\DoctrineFactory:

Factory discovery follows the same convention as Eloquent: given an entity App\Entities\User, Laravel looks for Database\Factories\Entities\UserFactory. The factory file lives in database/factories/Entities/UserFactory.php. Subdirectories under database/factories/ are mirrored from your entity namespace below App\.

Use it the same way you'd use any Laravel factory:

Feature Compatibility

Status of each Laravel factory feature when used through DoctrineFactory:

Feature Status Notes
Defining factories (definition()) ✅ works
make() ✅ works* Also calls EntityManager::persist() — see make() persists to the identity map
create() ✅ works
count() ✅ works
States (state(), state methods) ✅ works
Sequences (Sequence, sequence()) ✅ works
Has-many (has()) ✅ works
Belongs-to (for()) ✅ works
Magic relationship methods (hasPosts(), forUser()) ✅ works
Many-to-many (hasAttached()) N/A Set a Collection in factory state for plain M2M, or ->has() the pivot entity for M2M with pivot data
recycle() ✅ works
afterMaking() callback ✅ works
afterCreating() callback ✅ works
Polymorphic relationships N/A No direct Doctrine equivalent
trashed() / soft deletes N/A Doctrine has no built-in soft deletes

Differences From Eloquent

This package mirrors Laravel's Illuminate\Database\Eloquent\Factories\Factory API as closely as possible, so the Laravel docs work as your primary reference. The deviations below fall out of how Doctrine handles persistence, instantiation, and relationships — none of them are bugs, but they are not obvious from reading the Laravel docs.

make() persists to the identity map

Doctrine splits "register an entity with the EntityManager" (persist) from "write to the database" (flush). This package mirrors that split across make() and create():

This is the intended deviation from Laravel's make(), which leaves models entirely in-memory. If you call make() and later trigger an EntityManager::flush() yourself — even unintentionally — the made entities will be inserted at that point.

Two consequences worth knowing:

  1. No cascade: ['persist'] required. Related entities pulled in through the factory chain are persisted too, so a single flush() saves the whole graph without needing cascade rules on every association.
  2. Build a graph, then flush once. You can make() related entities, set them up however you need, then create() the root — everything flushes together.

Constructor-aware instantiation

Eloquent models share a uniform constructor; Doctrine entities don't. This package routes definition attributes through the entity constructor first, then sets remaining attributes via reflection.

A few rules to keep in mind:

Reflection bypasses setters

Non-constructor attributes are written directly to private properties via reflection — setters are not called. If a setter has side effects (validation, eventing, syncing a related collection), it won't fire. Either:

Relationships use entity instances, not IDs

for() and has() resolve to full entity instances:

This is fundamental to how Doctrine works — you set $post->user = $userEntity, never $post->user_id = 123.

Relationship names match entity property names

The second argument to ->for() (and ->has()) is the entity property name, not a database column, relationship table, or class name:

When the property name matches the related entity's basename (e.g. property $user of type User), magic methods like forUser() infer the property automatically and the second argument can be omitted.

Bidirectional Relationships

Doctrine relationships are often bidirectional — both entities reference each other. The factory only sets one side. The owning side persists correctly to the database, but reading the inverse side in-memory will return an empty collection until you flush and refresh:

This is the single biggest gotcha for developers coming from Eloquent, where models are dumb data containers and the inverse-side invariant doesn't exist.

Syncing with afterMaking()

Sync the inverse side from afterMaking(). Put it in configure() so it runs every time the factory is used:

Now $user->getPosts() returns the new post immediately — no flush, no refresh.

afterMaking() vs afterCreating()

Patterns

A handful of conventions that come up repeatedly when working with this package.

configure() for default callbacks

Override configure() to register afterMaking / afterCreating callbacks that should always run for a factory — most commonly, the bidirectional relationship sync shown above. It's called once per factory instance and is the right place for setup that every caller depends on.

Doctrine Collections in definitions

You can use ArrayCollection (or any Doctrine Collection) directly in a definition. The factory resolves any factories nested inside it:

This is also how you set up plain many-to-many relationships — see the Many-to-many row in the feature table.

Domain-specific helper methods

Wrap for() / state() / afterMaking() calls in domain-specific methods that accept multiple input types — entities, factories, or attribute arrays. Callers get a flexible API and you get a single place to keep the relationship logic:

This is more flexible than the built-in magic forUser(), which only accepts an attribute array.

Factory inheritance for entity hierarchies

When entities share an inheritance hierarchy, mirror it on the factories. An abstract base factory holds the shared definition; subclasses extend and add their own pieces:

EntityManager::refresh() escape hatch

When the in-memory state diverges from the database — most commonly because a related entity's collection was mutated after persistence — EntityManager::refresh($entity) re-fetches it from the database:

Manual sync via afterMaking() (above) avoids the round-trip but requires you to know which collections to update. refresh() is the safer fallback when you don't.

Development

Setup

Running Tests

Publishing new Versions

To publish a new version of the package, create a new tag and push it to the repository:

Go to Packagist and click on "Update" to update the package.

Explained Overrides

Most methods that this package overrides are documented next to an @override tag in the source, including the rationale for the change. If you're working on the package itself, the doc blocks are the canonical reference for why a given override exists.


All versions of laravel-doctrine-factory with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2|^8.3|^8.4
laravel-doctrine/orm Version ^3.0
laravel/framework Version ^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package stemble/laravel-doctrine-factory contains the following files

Loading the files please wait ...