Skip to content

Don't let EntityManager::getReference() return null #12389

@mariusjp

Description

@mariusjp

Feature Request

What

The EntityManager::getReference() should not return null when the found Entity does not match the given $entityName.

Why

We are using the power of getReference() to reduce the load on our application because we do the sanity checks ourselves and this way can easily associate relations to entities.
But because of the (possibly very wrong) situation of this mismatch the method returns null instead of throwing an Exception. And because of this, everywhere we use this we need to first check the output of the getReference() which kinda defeats the purpose in our opinion.

How

In the EntityManagerInterface remove the nullability of the the method getReference() and in the implementation EntityManager change the output of the if statement:

// Check identity map first, if its already in there just return it.
if ($entity !== false) {
    return $entity instanceof $class->name ? $entity : null;
}

to e.g.:

// Check identity map first, if its already in there just return it.
if ($entity !== false) {
    if (!$entity instanceof $class->name) {
        // Non-existing Exception, but something like this
        throw MismatchingEntity::create($class->name);
    }
    
    return $entity;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions