huttopia/doctrine

Add some features and fix to doctrine, without forking it

1.3.2 2017-10-06 09:25 UTC

This package is auto-updated.

Last update: 2024-04-07 20:23:38 UTC


README

version symfony symfony symfony Lines Total Downloads

huttopia/doctrine

Doctrine is a really good ORM, with nice features, we love it !

But, to be honest, no major version since 2 april 2015 (2.6 is here, but too much bugs for now), several bugs are not fixed and it takes too much time to create patch version when you create a PR to fix something.

So, we decided to create huttopia/doctrine, to fix what we need, without waiting for a release. We can add features too.

We decide to not fork Doctrine, because we want to follow Doctrine releases. Forking it now is nice and amazing, but in 2 years...

When we need to override a class, we do it with steevanb/composer-overload-class. That's a good way when you need it, without renaming namespace everywhere (we can't, that's not a fork ;)).

Changelog

Installation

Add it to your composer.json :

composer require huttopia/doctrine ^1.3.2

Register HuttopiaDoctrineBundle :

# app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles(): array
    {
        $bundles = [
            new Huttopia\Doctrine\Bridge\Symfony3\HuttopiaDoctrineBundle()
        ];

        return $bundles;
    }
}

Change Doctrine configuration :

# app/config/config.yml

doctrine:
    orm:
        repository_factory: huttopia.doctrine.repository_factory
        default_repository_class: Huttopia\Doctrine\Orm\EntityRepository

Configuration :

# app/config/config.yml

huttopia_doctrine:
    repository_factory_service: huttopia.doctrine.repository_factory # this is default value

Doctrine bugs

Bugs who are fixed or not fixed by Doctrine, for some reasons :

  • fixed by steevanb/doctrine-events Fix a Doctrine UnitOfwork bug with extraUpdates, who are not removed when you add and remove your entity before calling flush()
  • #6042 (not fixed) getId() lazy load entity if getId() is in trait : not fixed, just to remember why we don't use trait for getId()
  • #6110 (fixed) Clear $this->collection even when empty, to reset keys
  • #6509 (fixed here) PersistentCollection::clear() and removeElement() with orphanRemoval will remove your entity, although you don't want

For now, we fix 2.5.6 for doctrine/orm dependency : >2.5.6 and 2.6 has wrong tag (some dependencies are on dev-master, whithout version), some BC etc.

We will change it when 2.6 will be more tested.

Repositories as service

Yes, you need it too ;) Repositories as service is one of the biggest improvement.

Now, we can define your repository as service, with huttopia.repository tag :

services:
    bar_repository:
        class: Foo\Repository\BarRepository
        arguments: ['@service', '%parameter%']
        tags:
            - { name: huttopia.repository, entity: Foo\Bar }

You need to change extends Doctrine\ORM\EntityRepository by extends Huttopia\Doctrine\Orm\EntityRepository in your repositories.

Take care, our repository remove magic methods (findOneById() for example).

But it add a lot of methods :

  • getClassName(): string
  • getClassTableName(): string
  • createQueryBuilderWithoutSelect(string $alias, string $indexBy = null): QueryBuilder
  • get(int $id): Entity
  • getOneBy(array $criteria, array $orderBy = null): Entity
  • countAll(): int
  • countBy(array $params): int
  • findReadOnlyBy(array $criteria, array $fields = null, array $orderBy = null, $limit = null, $offset = null): array
  • getPartialReference(int $id)

Difference between find() and get(), and findOneBy() and getOneBy() : when entity is not found, find() will return null, as get() will throw an exception.

When you use PARTIAL, you can call createQueryBuilderWithoutSelect() instead of createQueryBuilder(), who will not select all root entity fields.

Remove useless discriminator in SQL for single table inheritance

With SINGLE_TABLE_INHERITANCE entities, Doctrine add discriminator columns into all SQL queries.

But if you want to query all entities, Doctrine add useless WHERE clause with discriminator : not really good for performances ;)

Huttopia\Doctrine\SqlWalker\IgnoreDiscriminator override Doctrine SqlWalker, to add WHERE clause only when needed.

Add it for a single query :

use Huttopia\Doctrine\SqlWalker\IgnoreDiscriminator

$queryBuilder
    ->getQuery()
    ->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, IgnoreDiscriminator::class);

Add it for all queries :

# app/AppKernel.php

class AppKernel
{
    public function boot(): void
    {
        parent::boot();

        foreach ($this->getContainer()->get('doctrine')->getManagers() as $manager) {
            if ($manager instanceof EntityManagerInterface) {
                $manager->getConfiguration()->setDefaultQueryHint(
                    Query::HINT_CUSTOM_OUTPUT_WALKER,
                    IgnoreDiscriminator::class
                );
            }
        }
    }
}

#6509 Fix PersistentCollection orphanRemoval management

When you call remove(), removeElement() or clear() on PersistentCollection, and your manyToOne configuration define orphanRemoval as true, PersistentCollection will add your deleted entity in UnitOfWork::$orphanRemovals.

flush() will read UnitOfWork::$orphanRemovals, and delete all entities, although they are deleted then added.

So, if you remove an entity, then add it again, then flush(), finally, your entity will be deleted.

To fix it, we override PersistentCollection, and unschedule orphanRemovals when we add entity to PersistentCollection.

See ComposerOverloadClass installation.

Override PersistentCollection to fix it :

{
    "extra": {
        "composer-overload-class": {
            "Doctrine\\ORM\\PersistentCollection": {
                "original-file": "vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php",
                "overload-file": "vendor/huttopia/doctrine/ComposerOverloadClass/Orm/PersistentCollection.php",
                "replace": true
            }
        }
    }
}

Enable steevanb/doctrine-events

It will replace EntityManager, to add some events : onCreateEntityOverrideLocalValues, onCreateEntityDefineFieldValues, onNewEntityInstance etc.

See Installation to install it.

Enable steevanb/doctrine-entity-merger

When you use PARTIAL in DQL, you retrieve only fields you need, instead of all Entity fields.

But, if you execute 2 PARTIAL on same entity, but not same fields, your final entity will not have second PARTIAL data, only first one is hydrated.

See Installation to install it.

Enable steevanb/doctrine-read-only-hydrator

See Benchmark, you will understand why we use ReadOnlyHydrator ;)

See Installation to install it.

Enable steevanb/doctrine-stats

steevanb/doctrine-stats add lot of statistics about queries, hydration time, lazy loaded entities, etc.

See Installation to install it.