find() returns an entity of id 1 if a string is passed as $id and starts with 1 #6917

Open
opened 2026-01-22 15:41:17 +01:00 by admin · 0 comments
Owner

Originally created by @kaamui on GitHub (Jan 27, 2022).

Bug Report

Q A
BC Break no
Version 2.7.1-DEV

Summary

With just this in a table "Version" => {id:1, value:"1.6.2"} :

dump($em->getRepository(Version::class)->find("1.5.4")); //returns the entity of id 1
dump($em->getRepository(Version::class)->find("1oiasdoijadajklsd")); //returns the entity of id 1
dump($em->getRepository(Version::class)->findOneById("1.5.4")); //returns the entity of id 1
dump($em->getRepository(Version::class)->findOneById("1oiasdoijadajklsd")); //returns the entity of id 1
dump($em->getRepository(Version::class)->find("2.0.6")); //returns null (but pretty sure if I have an entity with id 2 it will return it)

Current behavior

An entity of id intval(<insert_what_you_want_here>) is returned

How 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 ...

Originally created by @kaamui on GitHub (Jan 27, 2022). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.7.1-DEV #### Summary With just this in a table "Version" => {id:1, value:"1.6.2"} : ``` dump($em->getRepository(Version::class)->find("1.5.4")); //returns the entity of id 1 dump($em->getRepository(Version::class)->find("1oiasdoijadajklsd")); //returns the entity of id 1 dump($em->getRepository(Version::class)->findOneById("1.5.4")); //returns the entity of id 1 dump($em->getRepository(Version::class)->findOneById("1oiasdoijadajklsd")); //returns the entity of id 1 dump($em->getRepository(Version::class)->find("2.0.6")); //returns null (but pretty sure if I have an entity with id 2 it will return it) ``` #### Current behavior An entity of id `intval(<insert_what_you_want_here>)` is returned #### How 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 ...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6917