定制 huttopia/doctrine 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

huttopia/doctrine

Composer 安装命令:

composer require huttopia/doctrine

包简介

Add some features and fix to doctrine, without forking it

README 文档

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.

huttopia/doctrine 适用场景与选型建议

huttopia/doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.52k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 huttopia/doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 huttopia/doctrine 我们能提供哪些服务?
定制开发 / 二次开发

基于 huttopia/doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 6
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-27