EmicoEcommerce / Magento2TweakwiseExport-archived

Magento 2 module for Tweakwise export
Other
6 stars 16 forks source link

StockItemMapProvider can result in MySQL error when empty data set #86

Closed arnoudhgz closed 4 years ago

arnoudhgz commented 4 years ago

Magento: 2.3.4 Extension version: 2.1.10

The method getStockItemMap does a filtering on all items from the collection. We got a case that the result of $collection->getAllIds() was empty, which triggered a MySQL error when the filtering is done in Magento.

MySQL error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 2,   
  query was: SELECT `main_table`.*, `cp_table`.`type_id` FROM `cataloginventory_stock_item` AS `main_table`                                                                                                       
   INNER JOIN `catalog_product_entity` AS `cp_table` ON main_table.product_id = cp_table.entity_id AND (cp_table.created_in <= '1582643880' AND cp_table.updated_in > '1582643880') WHERE (`main_table`.`product  
  _id` IN())

Source: https://github.com/EmicoEcommerce/Magento2TweakwiseExport/blob/ece19e55869afc9be3a9398b28c56fae3fcb936f/src/Model/Write/Products/CollectionDecorator/StockData/StockItemMapProvider.php#L61

edwinljacobs commented 4 years ago

Out of curiosity before releasing the new version with your PR included. How does this scenario happen?

 if ($collection->count() === 0) {
            return [];
        }

        $entityIds = $collection->getAllIds();

        if (count($entityIds) === 0) {   // <---- included in your pull
            return [];
        }

Apparently $collection->count() is nonzero but $collection->getAllIds() is empty?

arnoudhgz commented 4 years ago

@edwinljacobs the getAllIds() method on the collection is also actually yours ;)

Under the hood it triggers this method (also yours) :

 public function getExported(): array
    {
        $result = [];
        foreach ($this as $entity) {
            if (!$entity->shouldExport()) {
                continue;
            }

            $result[$entity->getId()] = $entity;
        }
        return $result;
    } 

So if a batch of products are all skipped, you end up with an empty array.

edwinljacobs commented 4 years ago

Ah yes, should have clicked further in the method calls. I do see my name when annotating the code.... and hence should have known that unexported entities are still in the collection.

Case closed and released in https://github.com/EmicoEcommerce/Magento2TweakwiseExport/releases/tag/v1.5.6

With kind regards!

arnoudhgz commented 4 years ago

@edwinljacobs haha no worries, glad I could help.