Filter on JOIN #5465

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

Originally created by @yp28 on GitHub (Mar 15, 2017).

Hi, I'm having a problem filtering entities when retrieved through a join.

This is my case (simplified):
Let's say I have a User entity, and a Car entity. Both entities are soft-deletable (whether they're a good pattern or bad, I use them). When I find a User, the filter is applied, as well as with the Car entity. However, when I find a User and include the Car in a join, the filter is only applied to the User entity, and it returns the deleted Car entities as well.

I could not find any documentation as to whether this is expected (and wanted) behaviour, or a "bug". Shouldn't the filters also be applied to fetched relations?

Can anyone elaborate?

Edit: upon better search, this might be related to #6037

Originally created by @yp28 on GitHub (Mar 15, 2017). Hi, I'm having a problem filtering entities when retrieved through a join. This is my case (simplified): Let's say I have a User entity, and a Car entity. Both entities are soft-deletable (whether they're a good pattern or bad, I use them). When I `find` a User, the filter is applied, as well as with the Car entity. However, when I `find` a User and include the Car in a join, the filter is only applied to the User entity, and it returns the deleted Car entities as well. I could not find any documentation as to whether this is expected (and wanted) behaviour, or a "bug". Shouldn't the filters also be applied to fetched relations? Can anyone elaborate? Edit: upon better search, this might be related to #6037
admin added the BugMissing Tests labels 2026-01-22 15:08:18 +01:00
Author
Owner

@szymach commented on GitHub (Apr 18, 2017):

From the docs:

The filter functionality works on SQL level. Whether a SQL query is generated in a Persister, during lazy loading, in extra lazy collections or from DQL.

So this would seem like a bug. Did you try running the query with XDebug?

As for the PR you linked - do you have eager fetch as well?

@szymach commented on GitHub (Apr 18, 2017): From the [docs](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/filters.html#filters): > The filter functionality works on SQL level. Whether a SQL query is generated in a Persister, during lazy loading, in extra lazy collections or from DQL. So this would seem like a bug. Did you try running the query with XDebug? As for the PR you linked - do you have eager fetch as well?
Author
Owner

@yp28 commented on GitHub (Apr 19, 2017):

@szymach, we do have eager fetch configured for both entities that were involved.

I did not run it with XDebug enabled, we just reversed the queried the related entity as it was more important than the one we were query-ing.

@yp28 commented on GitHub (Apr 19, 2017): @szymach, we do have eager fetch configured for both entities that were involved. I did not run it with XDebug enabled, we just reversed the queried the related entity as it was more important than the one we were query-ing.
Author
Owner

@mssimi commented on GitHub (Apr 29, 2017):

I can confirm combining fetch="EAGER" and SQLFilter does not work well. SQLFilter is not aplied in this scenario.

Fix would be nice:D

@mssimi commented on GitHub (Apr 29, 2017): I can confirm combining fetch="EAGER" and SQLFilter does not work well. SQLFilter is not aplied in this scenario. Fix would be nice:D
Author
Owner

@lcobucci commented on GitHub (Apr 29, 2017):

@mssimi we're doing the best we can with the contributors that we have but since the failing test to reproduce it is available anyone could send a patch to fix the issue 😉

@lcobucci commented on GitHub (Apr 29, 2017): @mssimi we're doing the best we can with the contributors that we have but since the failing test to reproduce it is available anyone could send a patch to fix the issue 😉
Author
Owner

@lukaszjakubek commented on GitHub (Jun 30, 2017):

I confirm this on one-to-one bidirectional association. After owning side is soft deleted. When i get inversed side entity by simple select it also join owning side without filter applied.
Example:

    /**
     * Document original association
     *
     * @var \AppBundle\Entity\Document
     * @ORM\OneToOne(targetEntity="Document", inversedBy="draftDocument")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $draftOf = null;

    /**
     * Document draft association
     *
     * @var \AppBundle\Entity\Document
     * @ORM\OneToOne(targetEntity="Document", mappedBy="draftOf", cascade={"persist","remove"})
     */
    private $draftDocument = null;

This class has also SoftDeleteable extension applied from Atlantic18/DoctrineExtensions which adds SQL filters.

And when i try to select single entity i get SQL:

SELECT t0.title AS title_1, t0.body AS body_2, t0.slug AS slug_3, t0.version AS version_4, t0.draft AS draft_5, t0.version_info AS version_info_6, t0.id AS id_7, t0.created_at AS created_at_8, t0.updated_at AS updated_at_9, t0.created_by AS created_by_10, t0.updated_by AS updated_by_11, t0.deleted_at AS deleted_at_12, t0.clinic_id AS clinic_id_13, t0.parent_id AS parent_id_14, t0.draft_of_id AS draft_of_id_15, t16.title AS title_17, t16.body AS body_18, t16.slug AS slug_19, t16.version AS version_20, t16.draft AS draft_21, t16.version_info AS version_info_22, t16.id AS id_23, t16.created_at AS created_at_24, t16.updated_at AS updated_at_25, t16.created_by AS created_by_26, t16.updated_by AS updated_by_27, t16.deleted_at AS deleted_at_28, t16.clinic_id AS clinic_id_29, t16.parent_id AS parent_id_30, t16.draft_of_id AS draft_of_id_31 FROM app_document t0 LEFT JOIN app_document t16 ON t16.draft_of_id = t0.id WHERE t0.slug = ? AND ((t0.deleted_at IS NULL)) LIMIT 1

You can see: LEFT JOIN app_document t16 ON ... without AND (t0.deleted_at IS NULL) but WHERE has valid filter on t0 in the same time.

@lukaszjakubek commented on GitHub (Jun 30, 2017): I confirm this on one-to-one bidirectional association. After owning side is soft deleted. When i get inversed side entity by simple select it also join owning side without filter applied. Example: ``` /** * Document original association * * @var \AppBundle\Entity\Document * @ORM\OneToOne(targetEntity="Document", inversedBy="draftDocument") * @ORM\JoinColumn(onDelete="CASCADE") */ private $draftOf = null; /** * Document draft association * * @var \AppBundle\Entity\Document * @ORM\OneToOne(targetEntity="Document", mappedBy="draftOf", cascade={"persist","remove"}) */ private $draftDocument = null; ``` This class has also SoftDeleteable extension applied from Atlantic18/DoctrineExtensions which adds SQL filters. And when i try to select single entity i get SQL: ``` SELECT t0.title AS title_1, t0.body AS body_2, t0.slug AS slug_3, t0.version AS version_4, t0.draft AS draft_5, t0.version_info AS version_info_6, t0.id AS id_7, t0.created_at AS created_at_8, t0.updated_at AS updated_at_9, t0.created_by AS created_by_10, t0.updated_by AS updated_by_11, t0.deleted_at AS deleted_at_12, t0.clinic_id AS clinic_id_13, t0.parent_id AS parent_id_14, t0.draft_of_id AS draft_of_id_15, t16.title AS title_17, t16.body AS body_18, t16.slug AS slug_19, t16.version AS version_20, t16.draft AS draft_21, t16.version_info AS version_info_22, t16.id AS id_23, t16.created_at AS created_at_24, t16.updated_at AS updated_at_25, t16.created_by AS created_by_26, t16.updated_by AS updated_by_27, t16.deleted_at AS deleted_at_28, t16.clinic_id AS clinic_id_29, t16.parent_id AS parent_id_30, t16.draft_of_id AS draft_of_id_31 FROM app_document t0 LEFT JOIN app_document t16 ON t16.draft_of_id = t0.id WHERE t0.slug = ? AND ((t0.deleted_at IS NULL)) LIMIT 1 ``` You can see: `LEFT JOIN app_document t16 ON ...` without `AND (t0.deleted_at IS NULL)` but WHERE has valid filter on t0 in the same time.
Author
Owner

@lcobucci commented on GitHub (Aug 6, 2017):

@lukaszjakubek could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing.

You can find examples on 388afb46d0/tests/Doctrine/Tests/ORM/Functional/Ticket

@lcobucci commented on GitHub (Aug 6, 2017): @lukaszjakubek could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing. You can find examples on https://github.com/doctrine/doctrine2/tree/388afb46d0cb3ed0c51332e8df0de9e942c2690b/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@seyfer commented on GitHub (Oct 19, 2017):

Here is connected issue in Atlantic18/DoctrineExtensions with explanation
https://github.com/Atlantic18/DoctrineExtensions/issues/1463

and failing test case
https://github.com/Atlantic18/DoctrineExtensions/pull/1550

@seyfer commented on GitHub (Oct 19, 2017): Here is connected issue in Atlantic18/DoctrineExtensions with explanation https://github.com/Atlantic18/DoctrineExtensions/issues/1463 and failing test case https://github.com/Atlantic18/DoctrineExtensions/pull/1550
Author
Owner

@fuser82 commented on GitHub (Nov 30, 2019):

Any news from this?

@fuser82 commented on GitHub (Nov 30, 2019): Any news from this?
Author
Owner

@fleckiboy commented on GitHub (Aug 25, 2020):

Are there any news on this issue? I'm facing exactly the same problem after upgrading from v2.4.8 to v2.5.14.
SQL-Filter seems to be ignored for Eager Fetch Joins of Relations.

Is this Bug fixed in newer versions or is it still remaining in the follow up versions of Doctrine?

@fleckiboy commented on GitHub (Aug 25, 2020): Are there any news on this issue? I'm facing exactly the same problem after upgrading from v2.4.8 to v2.5.14. SQL-Filter seems to be ignored for Eager Fetch Joins of Relations. Is this Bug fixed in newer versions or is it still remaining in the follow up versions of Doctrine?
Author
Owner

@pana1990 commented on GitHub (Oct 2, 2020):

The same issue :(

@pana1990 commented on GitHub (Oct 2, 2020): The same issue :(
Author
Owner

@GaylordP commented on GitHub (Aug 18, 2021):

Same problem in 2021 :(

@GaylordP commented on GitHub (Aug 18, 2021): Same problem in 2021 :(
Author
Owner

@Miras4207 commented on GitHub (Jan 18, 2023):

We are also experiencing this issue and wish it could be resolved.

@Miras4207 commented on GitHub (Jan 18, 2023): We are also experiencing this issue and wish it could be resolved.
Author
Owner

@eigan commented on GitHub (Jan 18, 2023):

This issue is labeled as missing-tests, but here is a relevant testcase: https://github.com/doctrine/orm/pull/6037

Edit: This PR is actually mentioned in OP.

@eigan commented on GitHub (Jan 18, 2023): This issue is labeled as missing-tests, but here is a relevant testcase: https://github.com/doctrine/orm/pull/6037 Edit: This PR is actually mentioned in OP.
Author
Owner

@ahmed-bhs commented on GitHub (May 11, 2023):

Same problem in 2023 :(

@ahmed-bhs commented on GitHub (May 11, 2023): Same problem in 2023 :(
Author
Owner

@jeandanyel commented on GitHub (Sep 15, 2023):

Same problem here 🙁

@jeandanyel commented on GitHub (Sep 15, 2023): Same problem here 🙁
Author
Owner

@esserj commented on GitHub (Feb 7, 2025):

Recent PR #11707 merge could fix it in 2.20.3
Can any of you confirm that these cases are working now? so we can get this issue resolved.

@esserj commented on GitHub (Feb 7, 2025): Recent PR [#11707](https://github.com/doctrine/orm/pull/11707) merge could fix it in 2.20.3 Can any of you confirm that these cases are working now? so we can get this issue resolved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5465