PHPackages                             yapro/doctrine-ext - 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. yapro/doctrine-ext

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

yapro/doctrine-ext
==================

Doctrine extensions

v2.0(1y ago)011[1 issues](https://github.com/yapro/doctrine-ext/issues)proprietaryPHPPHP &gt;=8.0

Since May 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/yapro/doctrine-ext)[ Packagist](https://packagist.org/packages/yapro/doctrine-ext)[ RSS](/packages/yapro-doctrine-ext/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

Doctrine extensions
===================

[](#doctrine-extensions)

[![lib tests](https://github.com/yapro/doctrine-ext/actions/workflows/main.yml/badge.svg)](https://github.com/yapro/doctrine-ext/actions/workflows/main.yml/badge.svg)

This is a library for resolving common doctrine problems.

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

[](#installation)

Add as a requirement in your `composer.json` file or run

```
composer require yapro/doctrine-ext
```

As dev:

```
composer require apro/doctrine-ext dev-master
```

What inside
-----------

[](#what-inside)

1. Array to Doctrine Entity Hydrator
2. BigIntType - for native bigint supporting
3. ReloadDatabaseTrait - for Entities testing / data testing
4. EntityShouldToInvokeParentConstructTest - a test that verifies your entity that extending other class
5. EntityAutoFillTimeListener - for autofill fields (createdAt, updatedAt) of any Entities
6. ImportedObjectInterface - for specific autofill fields (createdAt, updatedAt) of any Entities
7. RequiredFieldsTrait - for Entities without an auto-generated ID (fields: createdAt, updatedAt)
8. AutoIdAndRequiredFieldsTrait - for Entities with an auto-generated ID (extends RequiredFieldsTrait)

### Array to Doctrine Entity Hydrator

[](#array-to-doctrine-entity-hydrator)

You can populate this doctrine entity object with an array, for example:

```
$data = [
    'name'        => 'Fred Jones',
    'email'       => 'fred@example.com',
    'company'     => 2,
    'permissions' => [1, 2, 3, 4]
];

$hydrator = new \YaPro\DoctrineExt\Hydrator\ArrayHydrator($entityManager);
$entity   = $hydrator->hydrate('App\Entity\User', $data);
```

You can even populate user with JSON API resource data ( [documentation](http://jsonapi.org/format/#document-resource-objects) )

```
$data = [
    'attributes'    => [
        'name'  => 'Fred Jones',
        'email' => 'fred@example.com',
    ],
    'relationships' => [
        'company'     => [
            'data' => ['id' => 1, 'type' => 'company'],
        ],
        'permissions' => [
            'data' => [
                ['id' => 1, 'type' => 'permission'],
                ['id' => 2, 'type' => 'permission'],
                ['id' => 3, 'type' => 'permission'],
                ['id' => 4, 'type' => 'permission'],
                ['name' => 'New permission']
            ]
        ]
    ]
];

$hydrator = new \YaPro\DoctrineExt\Hydrator\JsonApiHydrator($entityManager);
$entity   = $hydrator->hydrate('App\Entity\User', $data);
```

or like that:

```
$json = '{
   "parentId": 12,
   "title": "title1",
   "comments": [{"parentId": 23, "message": "str1"}, {"parentId": 34, "message": "str2"}]
}';
$hydrator = new \YaPro\DoctrineExt\Hydrator\SimpleHydrator($entityManager, new \YaPro\Helper\JsonHelper());
$entity   = $hydrator->fromJson(Article::class, $json);
```

See [more examples](tests/Functional/SimpleHydratorTest.php)

Notice: Doctrine ORM v2 is not supported after removing the [pmill/doctrine-array-hydrator](https://github.com/yapro/doctrine-ext/commit/efe74ed4df79f7450ff2e437cdab5e1ee3afae2a#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34L18) dependency.

### ReloadDatabaseTrait

[](#reloaddatabasetrait)

Example to usage ReloadDatabaseTrait

```
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use YaPro\DoctrineExt\ReloadDatabaseTrait;

class ExampleClassTest extends KernelTestCase
{
    use ReloadDatabaseTrait;

    protected static EntityManagerInterface $entityManager;

    public static function setUpBeforeClass()
    {
        self::$entityManager = self::$container->get(EntityManagerInterface::class);
    }

    public function myTest()
    {
        $this->truncateClass('My\User');
        $this->truncateTable('user_orders');
        $this->truncateAllTables();

        // ... some useful actions
    }
}
```

### BigIntType

[](#biginttype)

BigIntType - native php bigint supporting, example to configure:

```
doctrine:
    dbal:
        types:
            bigint: YaPro\DoctrineExt\DbalType\BigIntType
```

and usage:

```
