Remove/Manage exception from getSingleIdentifierFieldName() #6007

Closed
opened 2026-01-22 15:24:40 +01:00 by admin · 2 comments
Owner

Originally created by @arviteri on GitHub (Jul 1, 2018).

Originally assigned to: @Ocramius on GitHub.

Feature Request

Q A
New Feature yes
RFC yes/no
BC Break yes/no

Summary

After searching around the source code and trying to implement a feature for EasyAdmin to work with composite keys, I realized that no bundle that calls getSingleIdentifierFieldName() will work with composite keys due to the function throwing an exception.

For example, the reason why EasyAdmin doesn't work with composite keys is because it calls this function and because Pagerfanta calls this function as well. I'd like to suggest removing the exception because I haven't found a case where it could cause a fatal error.

Another suggestion would be to provide an argument (boolean) to pass which determines if the exception will be thrown. For example the edited function could look like...

   public function getSingleIdentifierFieldName(bool $throwsCompositeException = true)
     {
         if ($this->isIdentifierComposite && $throwsCompositeException) {
             throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name);
         }

         if ( ! isset($this->identifier[0])) {
             throw MappingException::noIdDefined($this->name);
         }

         return $this->identifier[0];
     }

This way every bundle that currently calls this function will be safe and unchanged due to the default value provided in the parameter.

I think that removing the exception or using the suggested edit would allow for many bundles that use Doctrine ORM to be able to implement support for composite keys.

Lastly

I do think the best option would be to remove the exception and let the function return the first field instead of throwing the composite key exception.

Originally created by @arviteri on GitHub (Jul 1, 2018). Originally assigned to: @Ocramius on GitHub. ### Feature Request <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | New Feature | yes | RFC | yes/no | BC Break | yes/no #### Summary <!-- Provide a summary of the feature you would like to see implemented. --> After searching around the source code and trying to implement a feature for EasyAdmin to work with composite keys, I realized that no bundle that calls `getSingleIdentifierFieldName()` will work with composite keys due to the function throwing an exception. For example, the reason why EasyAdmin doesn't work with composite keys is because it calls this function and because Pagerfanta calls this function as well. I'd like to suggest removing the exception because I haven't found a case where it could cause a fatal error. Another suggestion would be to provide an argument (boolean) to pass which determines if the exception will be thrown. For example the edited function could look like... public function getSingleIdentifierFieldName(bool $throwsCompositeException = true) { if ($this->isIdentifierComposite && $throwsCompositeException) { throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name); } if ( ! isset($this->identifier[0])) { throw MappingException::noIdDefined($this->name); } return $this->identifier[0]; } This way every bundle that currently calls this function will be safe and unchanged due to the default value provided in the parameter. I think that removing the exception or using the suggested edit would allow for many bundles that use Doctrine ORM to be able to implement support for composite keys. #### Lastly I do think the best option would be to remove the exception and let the function return the first field instead of throwing the composite key exception.
admin added the ImprovementInvalidQuestion labels 2026-01-22 15:24:40 +01:00
admin closed this issue 2026-01-22 15:24:40 +01:00
Author
Owner

@Ocramius commented on GitHub (Jul 1, 2018):

I fear that this is an X/Y question.

The exception is the correct outcome here. You cannot get a single identifier for something that has more than one identifier, and the Doctrine paginator can't operate on multiple identifier fields anyway.

Instead, the correct resolution is to NOT rely on this method at all when the mappings aren't known upfront.

Closing as invalid here.

@Ocramius commented on GitHub (Jul 1, 2018): I fear that this is an X/Y question. The exception is the correct outcome here. You cannot get a single identifier for something that has more than one identifier, and the Doctrine paginator can't operate on multiple identifier fields anyway. Instead, the correct resolution is to NOT rely on this method at all when the mappings aren't known upfront. Closing as `invalid` here.
Author
Owner

@arviteri commented on GitHub (Jul 2, 2018):

Thanks for clearing that up

@arviteri commented on GitHub (Jul 2, 2018): Thanks for clearing that up
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6007