ramsey / uuid-doctrine

:snowflake::file_cabinet: Allow the use of a ramsey/uuid UUID as Doctrine field type.
MIT License
895 stars 84 forks source link

OneToMany with fetch=EAGER fails #271

Open BenoitDuffez opened 4 months ago

BenoitDuffez commented 4 months ago

Similar to #264 but triggered with a join on two tables.

Description

Say you have entities A, B, and A has a OneToMany relation with B. I wanted to avoid multiple db lookups so I have added fetch: 'EAGER' on A's OneToMany attribute.

Steps to reproduce

Create 2 entities with binary uuid IDs and link them with a OneToMany relation with fetch: 'EAGER'

Expected behavior

Looking up A and all its B's would be done in one query.

Screenshots or output

Actual behavior: crash on vendor/doctrine/orm/src/Persisters/Entity/BasicEntityPerister.php:1911:

UnhandledMatchError HTTP 500 Internal Server Error Unhandled match case 16

private function getArrayBindingType(ParameterType|int|string $type): ArrayParameterType|int
{
    if (! $type instanceof ParameterType) {
        $type = Type::getType((string) $type)->getBindingType();
    }
    return match ($type) {
        ParameterType::STRING => ArrayParameterType::STRING,
        ParameterType::INTEGER => ArrayParameterType::INTEGER,
        ParameterType::ASCII => ArrayParameterType::ASCII,
    };
}

In our case $type is 16, which is ParameterType::BINARY.

Environment details

Additional context

Changing the function above with this:

private function getArrayBindingType(ParameterType|int|string $type): ArrayParameterType|int
{
    if (! $type instanceof ParameterType) {
        $type = Type::getType((string) $type)->getBindingType();
    }
    return match ($type) {
        ParameterType::STRING, ParameterType::BINARY => ArrayParameterType::STRING,
        ParameterType::INTEGER => ArrayParameterType::INTEGER,
        ParameterType::ASCII => ArrayParameterType::ASCII,
    };
}

solves both problems:

  1. it works
  2. there are 7 queries on my page vs 9 before, showing that eagerly fetching (e.g. joining tables on the 1st query) optimizes db access and increase performance