All Projects → steevanb → doctrine-read-only-hydrator

steevanb / doctrine-read-only-hydrator

Licence: GPL-3.0 license
Add SimpleObject and ReadOnly hydrators do Doctrine.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to doctrine-read-only-hydrator

DoctrineBehaviors
[DEPRECATED]
Stars: ✭ 11 (-79.63%)
Mutual labels:  doctrine
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+51.85%)
Mutual labels:  doctrine
doctrine-filter
Quickly add advanced filtering/searching and sorting capabilities to any resource in your APIs or Web apps that use Doctrine.
Stars: ✭ 27 (-50%)
Mutual labels:  doctrine
rector-doctrine
Rector upgrades rules for Doctrine
Stars: ✭ 37 (-31.48%)
Mutual labels:  doctrine
domain
A dependency-free package to help building a business domain layer
Stars: ✭ 33 (-38.89%)
Mutual labels:  doctrine
dbal-rds-data
A driver to use the aws aurora serverless rds data api in the doctrine database abstraction layer
Stars: ✭ 24 (-55.56%)
Mutual labels:  doctrine
symbok-bundle
Symfony annotations bundle
Stars: ✭ 50 (-7.41%)
Mutual labels:  doctrine
DoctrineMongoODMModule
Laminas Module for Doctrine MongoDB ODM
Stars: ✭ 83 (+53.7%)
Mutual labels:  doctrine
Lumen-Doctrine-DDD-Example
Domain Driven Design Application Example, built with Lumen 5.3 and Doctrine.
Stars: ✭ 72 (+33.33%)
Mutual labels:  doctrine
MostGenerator
Transformation cartridges for generating Symfony bundles from ModuleStudio models.
Stars: ✭ 21 (-61.11%)
Mutual labels:  doctrine
nette-oauth2-server-doctrine
Integration of The League of Extraordinary Packages' OAuth 2.0 Server into Nette Framework- Kdyby/Doctrine storage implementation
Stars: ✭ 13 (-75.93%)
Mutual labels:  doctrine
slim-doctrine
Slim-Doctrine managers integration
Stars: ✭ 16 (-70.37%)
Mutual labels:  doctrine
ErrorHeroModule
💎 A Hero for your Zend Framework/Laminas, and Expressive/Mezzio application to log ( DB and Mail ) and handle php errors & exceptions during Mvc process/between request and response
Stars: ✭ 47 (-12.96%)
Mutual labels:  doctrine
doctrine-extensions
Doctrine2 behavioral extension Transformable
Stars: ✭ 14 (-74.07%)
Mutual labels:  doctrine
doctrine-active-record
Object-oriented CRUD (create, read, update, delete) for Doctrine DBAL
Stars: ✭ 25 (-53.7%)
Mutual labels:  doctrine
doctrine-phpstorm-meta
PhpStorm meta data for expected arguments completion.
Stars: ✭ 35 (-35.19%)
Mutual labels:  doctrine
DoctrineTranslated
Translated strings for Doctrine
Stars: ✭ 12 (-77.78%)
Mutual labels:  doctrine
doctrine-orm-batcher
No description or website provided.
Stars: ✭ 27 (-50%)
Mutual labels:  doctrine
ip
Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.
Stars: ✭ 212 (+292.59%)
Mutual labels:  doctrine
mukadi-wordpress-bundle
Integrate wordpress and symfony in the same application
Stars: ✭ 24 (-55.56%)
Mutual labels:  doctrine

version doctrine php Lines Total Downloads Scrutinizer

doctrine-read-only-hydrator

When you retrieve data with Doctrine, you can get an array with values, or a fully hydrated object.

Hydration is a very slow process, who return same instance of entity if several hydrations have same entity to hydrate. It's fine when you want to insert / update / delete your entity. But when you just want to retrieve data without editing it (to show it in list for example), it's way to slow.

If you want to really retrieve data from your database, and don't get UnitOfWork reference : with Doctrine hydration you can't. Each query will not hydrate a new entity with data taken in your query result, it will return the first hydrated entity, known by UnitOfWork.

So, in case you don't need to modify your entity, you want to be really faster, or just retrieve data stored in your database, you can use SimpleObjectHydrator or ReadOnlyHydrator.

This hydrated entities can't be persisted / flushed, because they are not registered in UnitOfwork to be faster.

Nothing will be lazy loaded : to be faster, and because most of the time, you have to create a complete QueryBuilder, who return everything you need.

Changelog

Benchmark

This table show simple benchmark results (time and memory_get_peak_usage()), with 30, 1000 and 5000 entities retrieved from a MySQL 5.7 database, PHP 5.6.23 and Doctrine 2.5.4.

ArrayHydrator is used when you call $query->getArrayResult(), ObjectHydrator when you call $query->getResult(), $repository->findAll() or $repository->findBy().

SimpleObjectHydrator and ReadOnlyHydrator are provided with this lib, see example above.

Entities SQL request ArrayHydrator SimpleObjectHydrator ReadOnlyHydrator ObjectHydrator
30 0.26ms 1.05 ms, 28 mo 1.45 ms, 28 mo 1.78 ms, 28 mo 7.94 ms, 29 mo
1000 1.58 ms 29.75 ms, 32 mo 37.01 ms, 29 mo 43.26 ms, 32 mo 113.45 ms, 41 mo
5000 6.36 ms 164.76 ms, 48 mo 187.30 ms, 32 mo 228.89 ms, 46 mo 671.82 ms, 90 mo

benchmark

You can see hydration process is really slow ! For 5 000 entities, Doctrine ObjectHydrator is 100x slower than the SQL request...

As expected, getArrayResult() is the fastest way to retrieve data. But, you have to work with array, so you can't use entity methods.

ReadOnlyHydrator is 4x faster than Doctrine ObjectHydrator, and only 40% slower than ArrayHydrator (who is hard to use).

Is you want to be as fast as possible, but with an entity result instead of an array, SimpleObjectHydrator looks pretty good.

SimpleObjectHydrator

  • Hydrate your entity, with all selected fields in your QueryBuilder. If you try to access a non-loaded property, no exception will be throwned, you can call all accessors.
  • No lazy loading will be executed.
  • You can't persist or flush this entity.
  • Usefull when you want to be faster than Doctrine ObjectHydrator (and a little little bit more than ReadOnlyHydrator), you don't want to insert / update this entity, but doesn't ensure you can't access non-loaded property.

ReadOnlyHydrator

  • Hydrate a proxy of your entity, who throw an exception if you try to access a property who is not loaded by your QueryBuilder.
  • No lazy loading will be executed.
  • You can't persist or flush this entity.
  • Usefull when you want to be faster than Doctrine ObjectHydrator, you don't want to insert / update this entity, and be "sure" any access to a non-loaded property will throw an exception.

Example

# Foo\Repository\BarRepository

use steevanb\DoctrineReadOnlyHydrator\Hydrator\ReadOnlyHydrator;

class BarRepository
{
    public function getReadOnlyUser($id)
    {
        return $this
            ->createQueryBuilder('user')
            ->select('user', 'PARTIAL comments.{id, comment}')
            ->join('user.comments', 'comments')
            ->where('user.id = :id')
            ->setParameter('id', $id)
            ->getQuery()
            ->getResult(ReadOnlyHydrator::HYDRATOR_NAME);
    }
}

Installation

composer require steevanb/doctrine-read-only-hydrator ^2.3

Symfony 2.x or 3.x integration

# app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new steevanb\DoctrineReadOnlyHydrator\Bridge\ReadOnlyHydratorBundle\ReadOnlyHydratorBundle()
        ];
    }
}

Symfony 4.x or 5.x integration

# config/bundles.php

return [
    steevanb\DoctrineReadOnlyHydrator\Bridge\ReadOnlyHydratorBundle\ReadOnlyHydratorBundle::class => ['all' => true],
];

Manual integration

You need to register SimpleObjectHydrator and ReadOnlyHydrator to Doctrine\ORM\Configuration:

use steevanb\DoctrineReadOnlyHydrator\Hydrator\SimpleObjectHydrator;
use steevanb\DoctrineReadOnlyHydrator\Hydrator\ReadOnlyHydrator;

$configuration->addCustomHydrationMode(SimpleObjectHydrator::HYDRATOR_NAME, SimpleObjectHydrator::class);
$configuration->addCustomHydrationMode(ReadOnlyHydrator::HYDRATOR_NAME, ReadOnlyHydrator::class);

Integration with steevanb/doctrine-stats

steevanb/doctrine-stats add lots of statistics about Doctrine : number of mapped entities, number of lazy loaded entities, collapse and count same sql queries, show hydration time etc.

If you use this lib, you have to add SimpleObjectHydrator and ReadOnlyHydrator hydration times :

# composer.json

{
    "extra": {
        "composer-overload-class-dev": {
            "steevanb\\DoctrineReadOnlyHydrator\\Hydrator\\SimpleObjectHydrator": {
                "original-file": "vendor/steevanb/doctrine-read-only-hydrator/Hydrator/SimpleObjectHydrator.php",
                "overload-file": "vendor/steevanb/doctrine-read-only-hydrator/ComposerOverloadClass/Hydrator/SimpleObjectHydrator.php"
            },
            "steevanb\\DoctrineReadOnlyHydrator\\Hydrator\\ReadOnlyHydrator": {
                "original-file": "vendor/steevanb/doctrine-read-only-hydrator/Hydrator/ReadOnlyHydrator.php",
                "overload-file": "vendor/steevanb/doctrine-read-only-hydrator/ComposerOverloadClass/Hydrator/ReadOnlyHydrator.php"
            }
        }
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].