mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
find() returns an entity of id 1 if a string is passed as $id and starts with 1 #6918
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kaamui on GitHub (Jan 27, 2022).
Bug Report
Summary
With just this in a table "Version" => {id:1, value:"1.6.2"} :
Current behavior
An entity of id
intval(<insert_what_you_want_here>)is returnedHow to reproduce
Create a simple entity as mentioned. Fill the table as mentioned. Try to dump find results to see how it behaves.
Expected behavior
IMHO, it should return null in the case the PK is of type INT and the user performs a search with a non-int value. I don't know if an explicit call to intval is performed, or if it happens more implicitly, but I think, for a PK of type int,
is_numeric($id)or something more robust could be performed to return null if not true. For "1.5.4", maybe it could throw an exception because not of the type of the PK, idk ...@kafoso commented on GitHub (Jan 27, 2022):
Agreed.
Calling the function with
is_numeric('1.5')will still yieldint 1, though, because'1.5'is indeed numeric (a decimal).This regex should do the job for validating an integer:
/^(0|-?[1-9]\d*)$/@derrabus commented on GitHub (Jan 28, 2022):
ORM 2.7 is not supported anymore. Please reproduce your issue with a recent version of the ORM.
@beberlei commented on GitHub (Jan 28, 2022):
This is still the case with a recent ORM since this is just the type juggling applied here, passing a string gets converted to int.
I don't see that we fix this, because we cannot ensure this kind of safety for arbitrary types.
@kaamui commented on GitHub (Jan 28, 2022):
But you know the expected type for the column you search in right ? I agree that in most cases it cannot be resolved, but in the most common case where find() must search for a value of a PK that is a single column of type int, you can ensure that the given string is a int or return null.
Btw, I searched where thie conversion was made, in EntityManager.php, UnitOfWork.php, but could not find precisely where the conversion happens. Do you know precisely where the mixed $id becames an int in this case ?