magento2
magento2 copied to clipboard
addFilterToMap function is not working for certain columns
Preconditions and environment
Magento 2.4.5-p1
There an issue related with addFilterToMap function on custom grid collection used in custom admin grid. If you map column with name created_at
it will conflicts with same column in joined table in where
condition. It seems that new plugin Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter
in aroundAddFieldToFilter
function ignores AbstractDb's mapper _getMappedField
that called inside _translateCondition
. In this case you get error "SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous".
Steps to reproduce
- Create custom grid in admin based on table that has
created_at
column - Rewrite
_initSelect
to add join with another table that also hascreated_at
column and after that apply filter map$this->addFilterToMap('created_at', 'main_table.created_at');
- Apply filter on the grid by 'created_at' field or you can just apply
addFieldToFilter
bycreated_at
column programmatically on you collection
Expected result
filtering of a collection by mapped column created_at
works without errors and uses applied filter mapping
Actual result
applied filter mapping for created_at
column not working. Getting errorSQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous
.
Additional information
No response
Release note
No response
Triage and priority
- [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- [x] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
Hi @dzhuser. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:
- Summary of the issue
- Information on your environment
- Steps to reproduce
- Expected and actual results
Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:
@magento give me 2.4-develop instance
- upcoming 2.4.x release
For more details, review the Magento Contributor Assistant documentation.
Add a comment to assign the issue: @magento I am working on this
To learn more about issue processing workflow, refer to the Code Contributions.
- Join Magento Community Engineering Slack and ask your questions in #github channel.
:warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
:clock10: You can find the schedule on the Magento Community Calendar page.
:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.
:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel
Hi @engcom-Dash. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
-
- Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
Details
If the issue has a valid description, the labelIssue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until labelIssue: Format is valid
appears.
- Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
-
- Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
Issue: Clear Description
label to the issue by yourself.
- Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
-
- Add
Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.
- Add
-
- Verify that the issue is reproducible on
2.4-develop
branchDetails
- Add the comment@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-develop
branch, please, add the labelReproduced on 2.4.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
- Verify that the issue is reproducible on
- Join Magento Community Engineering Slack and ask your questions in #github channel.
@dzhuser @engcom-Dash I have replicated the issue and a single line fix.
Find line:
$fieldName = $subject->getConnection()->quoteIdentifier($field);
Replace with:
$fieldName = $subject->getConnection()->quoteIdentifier('main_table.' . $field);
I will create a PR soon. I need to sync my forked repository before that.
Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
-
[ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
Details
If the issue has a valid description, the labelIssue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until labelIssue: Format is valid
appears. -
[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
Issue: Clear Description
label to the issue by yourself. -
[ ] 3. Add
Component: XXXXX
label(s) to the ticket, indicating the components it may be related to. -
[ ] 4. Verify that the issue is reproducible on
2.4-develop
branchDetails
- Add the comment@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-develop
branch, please, add the labelReproduced on 2.4.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here! -
[ ] 5. Add label
Issue: Confirmed
once verification is complete. -
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Hello @dzhuser,
Thanks for the reporting and collaboration!
We have tried to reproduce the issue in the 2.4-develop branch and the issue is reproducible for us. The issue can be reproducible with the exact description mentioned in the main window.
Please refer to the screenshot below for reference:

Hence confirming the issue.
Thanks
:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-7770 is successfully created for this GitHub issue.
:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.
@ananth-iyer Thank you very much for solution, it works for me. What would be the best solution to apply this fix, I think applying the change directly in vendor/magento/module-sales/Plugin/Model/ResourceModel/Order/OrderGridCollectionFilter.php would fiendly Thank you for your reply
@ljr95 I am happy to know that my solution worked for you. You could create a patch file, then add an entry in composer.json for future deployments.
@ananth-iyer Thank you for your answer, can you give me the outline so that I can realize it?
@magento I am working on this
@ananth-iyer the thing is that \Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter::aroundAddFieldToFilter
is affecting filter created_at
of any table. So your solution fixes only the consequences but not the cause.
I think this plugin should only affect the very specific place that is needed but not every call of addFieldToFilter
I mean, this solution will add main_table.
to every created_at
field when filtering
@SerhiiMandrykin This error occurs only in this Plugin class and not in other places.
@ananth-iyer this is plugin on Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
and runs for other places
@SerhiiMandrykin Here the issue is Column 'created_at' in where the clause is ambiguous.
But $subject->getConnection()->quoteIdentifier is not saying from which table created_at
field should be used. So adding main_table.
will be specific for Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter
and solves it.
main_table is alias for sales_order_grid table.
quoteIdentifier method is not used in other plugin on Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
.
@ananth-iyer well this plugin affects all other grids
If you have the created_at column on your custom grid and haveaddFilterToMap
for the column - it won't work because of the plugin
Here's the flow:
- You filter by
created_at
- The method
addFieldToFilter
is going to be called - Here's where the plugin
\Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter::aroundAddFieldToFilter
comes up - The plugin prevents
addFieldToFilter
for your custom grid.
I'm trying to say that it is better to not set plugin on Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
because it is too common
@SerhiiMandrykin As per plugin name suggests: orderGridCollectionFilterPlugin
it should be for Magento\Sales\Model\ResourceModel\Order\Grid\Collection
, but previous developer add it for Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
and will cause issues in other places.
Should this be changed in adminhtml/di.xml
of Magento_Sales from Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
to Magento\Sales\Model\ResourceModel\Order\Grid\Collection
?
I will test it locally and if it works then push in the PR.
@ananth-iyer yes, i guess changing it to Magento\Sales\Model\ResourceModel\Order\Grid\Collection
would be great. thank you
@SerhiiMandrykin I changed it to Magento\Sales\Model\ResourceModel\Order\Grid\Collection
in latest commit.
@ananth-iyer thank you!
@ananth-iyer The problem with changing the plugin target to Magento\Sales\Model\ResourceModel\Order\Grid\Collection
is that both the CreditMemo and Invoice grids only extend Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
.
With your new target the plugin code for the created_at
and order_created_at
fields won't be executed for the CreditMemo and Invoice grids which can potentially cause unwanted results. (both of these grids have a created_at field)
So in this case we can create a class that extends Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
and then do all the staff in there instead of the plugin
Quite a few other grids depend on Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
and I think we could tweak the plugin to have it call $proceed($field, $condition)
all the time. I have to do further tests but I don't see the point of manually adding the sql clause there instead of letting the parent (and its custom logic potentially) do the work in addFieldToFilter.
That would give us something like:
I've looked at most of the inherited classes of Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
and the only problematic code I found is this one in Magento\Customer\Model\ResourceModel\Grid\Collection
:
It duplicates the logic of the plugin. At the moment the code if ($field === 'created_at')
is never called because the plugin doesn't call $proceed($field, $condition)
for created_at.
So by always calling $proceed($field, $condition)
in the filter and removing this if block in Magento\Customer\Model\ResourceModel\Grid\Collection
we can make sure that each concrete Grid Class has the ability to add fields to the query any way it wants without impacting the others.
Also if we do this the plugin could be refactored as a before plugin
Same technically dead code in Magento\Sales\Model\ResourceModel\Order\Grid\Collection
that could potentially be removed:
@fgerodez We should follow SOLID DESIGN PRINCIPALS over there: https://stackify.com/solid-design-principles/
Please feel free to edit in my PR.
I was hitting this problem last week. For now I removed the plugin in my modules etc/adminhtml/di.xml with this code:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Force disable date check plugin that breaks the filter -->
<!-- This should be fixed in Magento 2.4.7 -->
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<plugin name="orderGridCollectionFilterPlugin"
type="Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter"
disabled="true"/>
</type>
Also informed by client he should use the Datepicker when filtering because the check is now gone.
I won't/can't fix it myself, but; this plugin should look if there is a mapping set and add it before the fieldname, I don't think hardcoding main_table like suggested is the correct way to do this.
The issue still exists in 2.4.6-p3, is there any patch available to fix this issue?