To make assert($mapping instanceof ManyToManyOwningSideMapping) error more explicit #7493

Open
opened 2026-01-22 15:52:18 +01:00 by admin · 10 comments
Owner

Originally created by @olinox14 on GitHub (Mar 27, 2025).

Feature Request

replacing
4baa7bd252/src/Mapping/AssociationMapping.php (L138)

by something like :

if (!$mapping instanceof ManyToManyOwningSideMapping) {
    throw new \Exception(
        "Mapping error on field '" .
        $mapping->fieldName . '[' . $mapping->sourceEntity . ']' .
        "': joinTable can only be set on many-to-many owning side.");
}

Would ease the debugging of this error, as today, the assertion error does not provide any information about the cause of the error.

Originally created by @olinox14 on GitHub (Mar 27, 2025). ### Feature Request replacing https://github.com/doctrine/orm/blob/4baa7bd25218f363dc18286a9e1d32e2148b7fee/src/Mapping/AssociationMapping.php#L138 by something like : if (!$mapping instanceof ManyToManyOwningSideMapping) { throw new \Exception( "Mapping error on field '" . $mapping->fieldName . '[' . $mapping->sourceEntity . ']' . "': joinTable can only be set on many-to-many owning side."); } Would ease the debugging of this error, as today, the assertion error does not provide any information about the cause of the error.
Author
Owner

@greg0ire commented on GitHub (Mar 27, 2025):

Can you send a PR? If you do, read https://github.com/doctrine/orm/issues/11208 to select the right branch.

@greg0ire commented on GitHub (Mar 27, 2025): Can you send a PR? If you do, read https://github.com/doctrine/orm/issues/11208 to select the right branch.
Author
Owner

@greg0ire commented on GitHub (Mar 27, 2025):

Also, maybe it makes sense to do something similar for 4baa7bd252/src/Mapping/AssociationMapping.php (L132)

@greg0ire commented on GitHub (Mar 27, 2025): Also, maybe it makes sense to do something similar for https://github.com/doctrine/orm/blob/4baa7bd25218f363dc18286a9e1d32e2148b7fee/src/Mapping/AssociationMapping.php#L132
Author
Owner

@olinox14 commented on GitHub (Mar 29, 2025):

Done, here is the PR : https://github.com/doctrine/orm/pull/11896

@olinox14 commented on GitHub (Mar 29, 2025): Done, here is the PR : https://github.com/doctrine/orm/pull/11896
Author
Owner

@beberlei commented on GitHub (Mar 29, 2025):

Does this even happen? The assert seems to be to make ststic analysis happy

@beberlei commented on GitHub (Mar 29, 2025): Does this even happen? The assert seems to be to make ststic analysis happy
Author
Owner

@olinox14 commented on GitHub (Mar 29, 2025):

@beberlei I'm not sure about the static analysis, but it would certainly be more dev-friendly. In the current state, the dev only receives an AssertionError stating :

assert($mapping instanceof ManyToManyOwningSideMapping)

with no information about which relation is at fault or why. An explicit exception describing the origin of the problem and suggesting a solution would save time and trouble for the user.

Before :

assert($mapping instanceof ManyToManyOwningSideMapping)

After :

Mapping error on field 'myField [App\Entity\MyEntity]': joinTable can only be set on many-to-many owning side.
@olinox14 commented on GitHub (Mar 29, 2025): @beberlei I'm not sure about the static analysis, but it would certainly be more dev-friendly. In the current state, the dev only receives an AssertionError stating : assert($mapping instanceof ManyToManyOwningSideMapping) with no information about which relation is at fault or why. An explicit exception describing the origin of the problem and suggesting a solution would save time and trouble for the user. Before : assert($mapping instanceof ManyToManyOwningSideMapping) After : Mapping error on field 'myField [App\Entity\MyEntity]': joinTable can only be set on many-to-many owning side.
Author
Owner

@greg0ire commented on GitHub (Mar 29, 2025):

@olinox14 given your phrasing, I assumed this happened to you in real life. Is that really the case?

@greg0ire commented on GitHub (Mar 29, 2025): @olinox14 given your phrasing, I assumed this happened to you in real life. Is that really the case?
Author
Owner

@olinox14 commented on GitHub (Mar 29, 2025):

@greg0ire Yeah, you're right :)

I was upgrading a legacy project, with hundreds of relations, and encountered this assertion error. I had to resort to using xdebug to find the culprit, hence the issue.

@olinox14 commented on GitHub (Mar 29, 2025): @greg0ire Yeah, you're right :) I was upgrading a legacy project, with hundreds of relations, and encountered this assertion error. I had to resort to using xdebug to find the culprit, hence the issue.
Author
Owner

@olinox14 commented on GitHub (Mar 29, 2025):

@beberlei Oh right, sorry, I just realized what you meant to say, that these asserts were to quiet phpstan or whatever to complaining. Sorry for the hasty reply. Well, like I was saying previously, you can run into this assertion error if you misplace the JoinTable attribute.

@olinox14 commented on GitHub (Mar 29, 2025): @beberlei Oh right, sorry, I just realized what you meant to say, that these asserts were to quiet phpstan or whatever to complaining. Sorry for the hasty reply. Well, like I was saying previously, you can run into this assertion error if you misplace the JoinTable attribute.
Author
Owner

@greg0ire commented on GitHub (Mar 30, 2025):

Maybe you could add a test showing exactly that situation in your PR

@greg0ire commented on GitHub (Mar 30, 2025): Maybe you could add a test showing exactly that situation in your PR
Author
Owner

@olinox14 commented on GitHub (Mar 31, 2025):

Done, I've added two unit tests for each of the new exceptions, just waiting for the CI to run

@olinox14 commented on GitHub (Mar 31, 2025): Done, I've added two unit tests for each of the new exceptions, just waiting for the CI to run
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7493