magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.48k stars 9.29k forks source link

main_table prefix on exception company_name #31764

Open DavaGordon opened 3 years ago

DavaGordon commented 3 years ago

Preconditions (*)

  1. Magento 2.3.6 B2B
  2. Magento 2.4 B2B

Steps to reproduce (*)

  1. Login to admin panel
  2. Customer > All Customers
  3. Show company name column
  4. Filter by company name

After further investigation and collaboration i have found the cause for the issue, to replicate this issue.

A new collection is needed with an add a join from a field from the custom table to the customer grid collection, and try to filter the collection with that field.

Expected result (*)

  1. In 2.4-develop without B2B features enabled - Company filter is working fine with Billing/shipping address company name.

Actual result (*)

After installing B2B, Company name is removed for all existing normal customers from Customers page. For Company Account customers - Company name is displayed in the Company column but filtering with the same company name is not working. Filtering with billing / shipping Address - Company name works.

[2021-01-19 16:25:17] report.CRITICAL: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.company_name' in 'where clause', query was: SELECT COUNT(*) FROM `customer_grid_flat` AS `main_table`
 LEFT JOIN `directory_country_region` AS `rct` ON rct.region_id=`main_table`.`billing_region_id`
 LEFT JOIN `directory_country_region_name` AS `rnt` ON rnt.region_id=`main_table`.`billing_region_id` AND rnt.locale='en_GB'
 LEFT JOIN `company_advanced_customer_entity` AS `company_customer` ON company_customer.customer_id = main_table.entity_id
 LEFT JOIN `company` ON company.entity_id = company_customer.company_id WHERE (`main_table`.`company_name` LIKE '%naveo%') {"exception":"[object] (Zend_Db_Statement_Exception(code: 42): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.company_name' in 'where clause', query was: SELECT COUNT(*) FROM `customer_grid_flat` AS `main_table`
 LEFT JOIN `directory_country_region` AS `rct` ON rct.region_id=`main_table`.`billing_region_id`
 LEFT JOIN `directory_country_region_name` AS `rnt` ON rnt.region_id=`main_table`.`billing_region_id` AND rnt.locale='en_GB'
 LEFT JOIN `company_advanced_customer_entity` AS `company_customer` ON company_customer.customer_id = main_table.entity_id
 LEFT JOIN `company` ON company.entity_id = company_customer.company_id WHERE (`main_table`.`company_name` LIKE '%naveo%') at /Users/Dava/Sites/espo/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.company_name' in 'where clause' at /Users/Dava/Sites/espo/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91)"} []

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @DavaGordon. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


: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, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

gabrieldagama commented 3 years ago

Hi @DavaGordon, I couldn't reproduce this issue on 2.4, can you confirm the steps and how to reproduce such problem?

Thanks.

DavaGordon commented 3 years ago

Magneto 2 EE (2.3.6) / B2B Module / Magneto Cloud

Steps Admin Login > Customers

Filter by company name issue occurred reason for this is the reference of company name was coming from the company table which was not in the main table so when we filtered by company we received this issue. this was tested across 3 seperate websites all using similar environment specs and with all none standard magento modules disabled.

Outcome Unable to filter by company name. after some investigation i found that the original code

if (is_string($field) && count(explode('.', $field)) === 1) {

Always returned true as a result the it returning true the prefix of main_table was added to company_name which if you look at the overall code did no exist in the customer_grid_flat table as we had the company module active so the code should have returned false and retrieved the value from the

company.company_name rather than customer_grid_flat(main_table).company_name

The code below which was put in place on all 3 environments checks the main_table index to check if the column is valid if returns true it will append the main_table prefix otherwise the reference will look in the correct place which is the company table

if (is_string($field) && in_array($field, $this->getFulltextIndexColumns())) {

Tested 3 environments no link in code base 3 seperate companies all same issue. fix resolves issue for all 3

danieluniform commented 2 years ago

This bug occurs in Magento 2.3.7-pt2 as well. Default Magento does not have Company fields setup but once customers are linked to companies, I believe it's not hard to reproduce this error.

danieluniform commented 2 years ago

Solved this on our platform with a before plugin.

/**
     * Fix Magento 2.3.7 problem of not being able to locate the company_name field.
     *
     * @param \Magento\Customer\Model\ResourceModel\Grid\Collection $subject
     * @param $field
     * @param $condition
     * @return array
     */
    public function beforeAddFieldToFilter(\Magento\Customer\Model\ResourceModel\Grid\Collection $subject, $field, $condition) {
        if (strpos($field, 'company_name') !== false ) {
            $field = 'company.company_name';
        }
        return [$field, $condition];
    }
danieluniform commented 2 years ago

It's not really necessary to reproduce this ticket in order to see that this problem exists.

The list of columns in customer_grid_flat is : ["name","email","created_in","taxvat","shipping_full","billing_full","billing_firstname","billing_lastname","billing_telephone","billing_postcode","billing_region","billing_city","billing_fax","billing_company"]

By default, company_name (as in the search fields) is not in customer_grid_flat. Thus, filtering by "company" breaks as it's not a part of the customer index. This should probably be fixed by someone on the team.

apedicdev commented 2 years ago

@danieluniform did u try reindexing? being a flat table it should add the missing column as it happened for me

m2-assistant[bot] commented 2 years ago

Hi @engcom-November. 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:

engcom-November commented 2 years ago

Verified the issue on Magento 2.4-develop all instances : Enterprise and B2B but observed different behavior while verifying the issue but no exception/errors observed in logs. Scenario 1: 2.4-develop branch without B2B: Customers - All Customers - Filtering with Company column (updated with billing / Shipping Addresses - Company name) working fine. Address company name displayed in the grid - No issue Scenario 2: B2B instance: After installing B2B modules - Company name got removed for all the existing customers from the All customers page. Created a Company account from front end - and verified Admin - Customers - All Customers page - Company name is updated in Company column. however filtering with Company name is not working. Filtering with Address - Company name is working image image

image

github-jira-sync-bot commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-November. Thank you for verifying the issue.
Issue Available: @engcom-November, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

engcom-Bravo commented 2 months ago

Hello,

Internal team has started to work on it

Thanks.