PHPackages                             cube/doctrine-entity-factory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. cube/doctrine-entity-factory

AbandonedLibrary[Database &amp; ORM](/categories/database)

cube/doctrine-entity-factory
============================

Provides an interface to use the factory pattern for Doctrine entity construction

1.0.0(9y ago)1382BSD-3-ClausePHPPHP ^5.6|^7.0

Since Aug 18Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Cube-Solutions/doctrine-entity-factory)[ Packagist](https://packagist.org/packages/cube/doctrine-entity-factory)[ RSS](/packages/cube-doctrine-entity-factory/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Doctrine Entity Factory
=======================

[](#doctrine-entity-factory)

[![Build Status](https://camo.githubusercontent.com/b6bab31acccd87f8e48dd576796db010386d54f17824eaeb0bf6fe4c71269deb/68747470733a2f2f7472617669732d63692e6f72672f437562652d536f6c7574696f6e732f646f637472696e652d656e746974792d666163746f72792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Cube-Solutions/doctrine-entity-factory)[![Code Coverage](https://camo.githubusercontent.com/721b6a9050cf6f84e83166e07580ec36dc61d7f52a4bae9933ba74b99a336e7d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f67736f6d6f7a612f646f637472696e652d656e746974792d666163746f72792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gsomoza/doctrine-entity-factory/?branch=master)[![Packagist](https://camo.githubusercontent.com/950b4c9d4bf0e993dd491a51981d7c8f88d2f2c90240eec2bf26f3a94cd7c556/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f637562652f646f637472696e652d656e746974792d666163746f72792e737667)](https://packagist.org/packages/cube/doctrine-entity-factory)[![Author](https://camo.githubusercontent.com/8222d6eef0fd941ecf7f9af423740d2b4255ecd50069439241f2fa79f026d1f8/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406761627269656c5f736f6d6f7a612d626c75652e737667)](https://twitter.com/gabriel_somoza)[![License](https://camo.githubusercontent.com/7031fc019105c66a455a2fe0e0b7f7e4395fcccc773bf6aabf07f7fe069b440a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f637562652f646f637472696e652d656e746974792d666163746f72792e737667)](https://github.com/cube/doctrine-entity-factory/blob/master/LICENSE)

A factory pattern for creating new Doctrine entities. Since Doctrine 2.0 entities may be created which use the constructor. In order to create a standard factory pattern which may be implemented across libraries which implement Doctrine this repository provides an interface for factories to create an entity which is to be newly persisted to the object manager.

Installation
------------

[](#installation)

Installation of this module uses composer. For composer documentation, please refer to [getcomposer.org](http://getcomposer.org/).

```
$ php composer.phar require cube/doctrine-entity-factory
```

What Problem does this Solve?
-----------------------------

[](#what-problem-does-this-solve)

Let's say you have a class that, at some point, has to instantiate a new Doctrine entity. A good example is DoctrineResource from the zfcampus/zf-apigility-admin project, simplified below:

```
class DoctrineResource
{
  public function create($data)
  {
    $entityClass = $this->getEntityClass();
    // ...
    $entity = new $entityClass(); // notice this line
    // ...
    return $entity;
  }
}
```

The problem with the code above is that their users can't instantiate any entities that have required parameters in the constructor. They also can't use a named constructor to instantiate the object if they wanted to. There could be many reasons why they have required parameters in their entity's constructor, but since Doctrine is designed to allow that then so should you. Moreover, in the example above `DoctrineResource` is acting as the de-facto factory for the entities, which is one responsibility too many for that class.

The solution is to decouple the instantiation of your user's Doctrine entities from your library. So we created an interface, `EntityFactoryInterface`, that can be used by any library that needs a new instance of a Doctrine entity, but wants to delegate the process of actually building that entity. Your updated class would look as follows:

```
use Cube\DoctrineEntityFactory\EntityFactoryInterface;
class DoctrineResource
{
  /**
    * @var EntityFactoryInterface Set e.g. via DI, can default to SimpleEntityFactory
    *                             to avoid BC breaks
    */
  private $entityFactory;

  public function create($data)
  {
    $entityClass = $this->getEntityClass();
    // ...
    $entity = $this->entityFactory->get($entityClass);
    // ...
    return $entity;
  }
}
```

And now your class doesn't need to actually instantiate the Doctrine entity!

Yet Another Library (roll eyes)?
--------------------------------

[](#yet-another-library-roll-eyes)

Yes, because the use-case described above happens quite often actually around several Doctrine libraries. By providing a decentralized interface we can make all of these other libraries delegate instantiation to the same type of factory.

This library also provides two simple implementations:

- `SimpleEntityFactory`: the SOLID equivalent to doing `new $entityClass`
- `BypassConstructorEntityFactory`: will build the entity using reflection to entirely bypass the constructor.

Other implementations are possible, and the most common will probably be end-user factories that know how to instantiate their entities very well.

Is This Library for Me?
-----------------------

[](#is-this-library-for-me)

This library is for you if you're distributing code that needs a new instance of a Doctrine entity, but you don't personally have (or want!) any knowledge or control over HOW that entity must be instantiated.

License
-------

[](#license)

See [LICENSE.md](https://github.com/Cube-Solutions/doctrine-entity-factory/blob/master/LICENSE.md)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3603d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f00ae1c2b13428075ab880d5fec7e857f1e9446cb401dc6097ec947a81122ec5?d=identicon)[gabriel.somoza](/maintainers/gabriel.somoza)

---

Top Contributors

[![gsomoza](https://avatars.githubusercontent.com/u/106219?v=4)](https://github.com/gsomoza "gsomoza (12 commits)")[![TomHAnderson](https://avatars.githubusercontent.com/u/493920?v=4)](https://github.com/TomHAnderson "TomHAnderson (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cube-doctrine-entity-factory/health.svg)

```
[![Health](https://phpackages.com/badges/cube-doctrine-entity-factory/health.svg)](https://phpackages.com/packages/cube-doctrine-entity-factory)
```

###  Alternatives

[slevomat/coding-standard

Slevomat Coding Standard for PHP\_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.

1.5k130.4M2.1k](/packages/slevomat-coding-standard)[doctrine/coding-standard

The Doctrine Coding Standard is a set of PHPCS rules applied to all Doctrine projects.

31914.8M819](/packages/doctrine-coding-standard)[dereuromark/cakephp-ide-helper

CakePHP IdeHelper Plugin to improve auto-completion

1882.3M40](/packages/dereuromark-cakephp-ide-helper)[humanmade/coding-standards

Human Made Coding Standards

160437.4k57](/packages/humanmade-coding-standards)[thamaraiselvam/mysql-import

Import .sql file into a database using mysqli

3946.2k1](/packages/thamaraiselvam-mysql-import)[youwe/testing-suite

Contains Youwe's default testing packages for php.

13186.2k8](/packages/youwe-testing-suite)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
