FindOneBy not following criteria #6969

Open
opened 2026-01-22 15:42:23 +01:00 by admin · 2 comments
Owner

Originally created by @AndresConpas on GitHub (Apr 29, 2022).

Bug Report

Q A
BC Break No
Version 2.12.1

Summary

I am using FindOneBy and Doctrine returns an object that only matches one of 2 iterations of the criteria array, meaning it is behaving like an OR instead of an AND condition.

Current behavior

Returns an object that matches only ONE criteria

$em->getRepository(Test::class)->findOneBy(["testCode" => "001", "testName" => "MY TEST"]);

Returning an existing object that only differs from the corrent one in TestCode:

App\Entity\Issue\Test {#XXXXX ▼ ▼
  -TestTypeCode: "P356C"
  -TestCode: "HEY" <--- The error
  -testName: "MY TEST"
  -UpdatedDate: DateTime ...
}

How to reproduce

Not so sure if this will happen all the time, but this is certainly doing this for me at the moment. I have a normal DB with no weird things going on. Using a query in SQL returns the expected result, obviously in an array and not an object, but the correct result nonetheless.

Using the findOneBy() method should be enough, in my case the BD is big, with a good ammount of rows to search on, in case this is useful info.

Expected behavior

Returning the correct object:

App\Entity\Issue\Test {#XXXXX ▼ ▼
  -TestTypeCode: "P356C"
  -TestCode: "001" <--- The correct one
  -testName: "MY TEST"
  -UpdatedDate: DateTime ...
}

Originally created by @AndresConpas on GitHub (Apr 29, 2022). ### Bug Report | Q | A |------------ | ------ | BC Break | No | Version | 2.12.1 #### Summary I am using FindOneBy and Doctrine returns an object that only matches one of 2 iterations of the criteria array, meaning it is behaving like an OR instead of an AND condition. #### Current behavior Returns an object that matches only ONE criteria `$em->getRepository(Test::class)->findOneBy(["testCode" => "001", "testName" => "MY TEST"]);` Returning an existing object that only differs from the corrent one in TestCode: ``` App\Entity\Issue\Test {#XXXXX ▼ ▼ -TestTypeCode: "P356C" -TestCode: "HEY" <--- The error -testName: "MY TEST" -UpdatedDate: DateTime ... } ``` #### How to reproduce Not so sure if this will happen all the time, but this is certainly doing this for me at the moment. I have a normal DB with no weird things going on. Using a query in SQL returns the expected result, obviously in an array and not an object, but the correct result nonetheless. Using the `findOneBy()` method should be enough, in my case the BD is big, with a good ammount of rows to search on, in case this is useful info. #### Expected behavior Returning the correct object: ``` App\Entity\Issue\Test {#XXXXX ▼ ▼ -TestTypeCode: "P356C" -TestCode: "001" <--- The correct one -testName: "MY TEST" -UpdatedDate: DateTime ... } ```
Author
Owner

@AndresConpas commented on GitHub (May 5, 2022):

It seems that using a composite primary key in the Entity was the solution, since both columns could have repeated values but the combination of both was indeed unique. I still do not quite understand why this affects the findOneBy() method when I am searching for both values though.

@AndresConpas commented on GitHub (May 5, 2022): It seems that using a **composite primary key** in the Entity was the solution, since both columns could have repeated values but the combination of both was indeed unique. I still do not quite understand why this affects the `findOneBy()` method when I am searching for both values though.
Author
Owner

@cpxPratik commented on GitHub (Oct 20, 2023):

I am having same issue. Both findBy() and findOneBy() gets last one that matched only first criteria ignoring other criteria.

@cpxPratik commented on GitHub (Oct 20, 2023): I am having same issue. Both `findBy()` and `findOneBy()` gets last one that matched only first criteria ignoring other criteria.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6969