magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

addFilterToMap function is not working for certain columns

Open dzhuser opened this issue 2 years ago • 29 comments

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

  1. Create custom grid in admin based on table that has created_at column
  2. Rewrite _initSelect to add join with another table that also has created_at column and after that apply filter map $this->addFilterToMap('created_at', 'main_table.created_at');
  3. Apply filter on the grid by 'created_at' field or you can just apply addFieldToFilter by created_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”.

dzhuser avatar Jan 10 '23 10:01 dzhuser

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.


: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

m2-assistant[bot] avatar Jan 10 '23 10:01 m2-assistant[bot]

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:

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
      DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
    1. 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.
    1. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced 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!

m2-assistant[bot] avatar Jan 10 '23 11:01 m2-assistant[bot]

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

ananth-iyer avatar Jan 18 '23 11:01 ananth-iyer

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

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: 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 branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced 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.

m2-assistant[bot] avatar Jan 20 '23 13:01 m2-assistant[bot]

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:

image

Hence confirming the issue.

Thanks

engcom-Hotel avatar Jan 20 '23 13:01 engcom-Hotel

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-7770 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Jan 20 '23 13:01 github-jira-sync-bot

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

m2-assistant[bot] avatar Jan 20 '23 13:01 m2-assistant[bot]

@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 avatar Feb 06 '23 11:02 ljr95

@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 avatar Feb 06 '23 14:02 ananth-iyer

@ananth-iyer Thank you for your answer, can you give me the outline so that I can realize it?

ljr95 avatar Feb 06 '23 15:02 ljr95

@magento I am working on this

ananth-iyer avatar Feb 08 '23 08:02 ananth-iyer

@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

SerhiiMandrykin avatar Feb 22 '23 10:02 SerhiiMandrykin

I mean, this solution will add main_table. to every created_at field when filtering

SerhiiMandrykin avatar Feb 22 '23 10:02 SerhiiMandrykin

@SerhiiMandrykin This error occurs only in this Plugin class and not in other places.

ananth-iyer avatar Feb 27 '23 11:02 ananth-iyer

@ananth-iyer this is plugin on Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult and runs for other places

SerhiiMandrykin avatar Feb 27 '23 11:02 SerhiiMandrykin

@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 avatar Feb 28 '23 14:02 ananth-iyer

@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:

  1. You filter by created_at
  2. The method addFieldToFilter is going to be called
  3. Here's where the plugin \Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter::aroundAddFieldToFilter comes up
  4. The plugin prevents addFieldToFilter for your custom grid. image

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 avatar Mar 01 '23 07:03 SerhiiMandrykin

@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 avatar Mar 01 '23 11:03 ananth-iyer

@ananth-iyer yes, i guess changing it to Magento\Sales\Model\ResourceModel\Order\Grid\Collection would be great. thank you

SerhiiMandrykin avatar Mar 01 '23 11:03 SerhiiMandrykin

@SerhiiMandrykin I changed it to Magento\Sales\Model\ResourceModel\Order\Grid\Collection in latest commit.

ananth-iyer avatar Mar 03 '23 02:03 ananth-iyer

@ananth-iyer thank you!

SerhiiMandrykin avatar Mar 03 '23 07:03 SerhiiMandrykin

@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)

fgerodez avatar Mar 27 '23 10:03 fgerodez

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

SerhiiMandrykin avatar Mar 27 '23 11:03 SerhiiMandrykin

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:

image

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:

image

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.

fgerodez avatar Mar 27 '23 12:03 fgerodez

Also if we do this the plugin could be refactored as a before plugin

fgerodez avatar Mar 27 '23 12:03 fgerodez

Same technically dead code in Magento\Sales\Model\ResourceModel\Order\Grid\Collection that could potentially be removed:

image

fgerodez avatar Mar 27 '23 13:03 fgerodez

@fgerodez We should follow SOLID DESIGN PRINCIPALS over there: https://stackify.com/solid-design-principles/

Please feel free to edit in my PR.

ananth-iyer avatar Mar 30 '23 11:03 ananth-iyer

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.

mdesplenter avatar Sep 07 '23 12:09 mdesplenter

The issue still exists in 2.4.6-p3, is there any patch available to fix this issue?

pradeepkanaka avatar Feb 28 '24 14:02 pradeepkanaka