1

I have a custom module and i want to define a Mysql Function in Setup/InstallSchema.php:

    $query = 'DELIMITER $$
        DROP FUNCTION IF EXISTS getQuantity;
        CREATE FUNCTION getQuantity(PID INT) RETURNS INT
        BEGIN DECLARE sum_qty INT;
        SELECT sum(qty) INTO sum_qty FROM
        (SELECT qty FROM cataloginventory_stock_item WHERE product_id = PID
            UNION ALL
            (SELECT IFNULL(sum(qty),0) as qty FROM cataloginventory_stock_item
                WHERE product_id IN (SELECT product_id FROM catalog_product_super_link WHERE parent_id = PID))) qty;
        RETURN sum_qty;
        END;
        $$ DELIMITER;';

    $installer->getConnection()->query($query);

When run setup:upgrade, it throws an exception:

Multiple queries can't be executed. Run a single query and try again.

How can I fix it? The function \Magento\Framework\DB\Adapter\Pdo\MySql::multiQuery() is deprecated and I don't want to use it.

1 Answer 1

0

You can use like.

$sql = "CREATE
                SQL SECURITY INVOKER
                VIEW {$viewToLegacyIndex}
                  AS
                    SELECT
                    DISTINCT    
                      legacy_stock_status.product_id,
                      legacy_stock_status.website_id,
                      legacy_stock_status.stock_id,
                      legacy_stock_status.qty quantity,
                      legacy_stock_status.stock_status is_salable,
                      product.sku
                    FROM {$legacyStockStatusTable} legacy_stock_status
                      INNER JOIN {$productTable} product
                        ON legacy_stock_status.product_id = product.entity_id;";
        $this->schemaSetup->getConnection()->query($sql);

you can get reference from core magento file "vendor/magento/module-inventory-catalog/Setup/Patch/Schema/CreateLegacyStockStatusView.php" .

https://github.com/magento/inventory/blob/40e199b82fd2e1cdd07541f856aa45a8a2e40fc3/InventoryCatalog/Setup/Patch/Schema/CreateLegacyStockStatusView.php#L68-L83

2
  • HI @S.P thank you for this code. I try to use it but its not working, can you please give information about that.
    – NoOn
    Commented Nov 30, 2022 at 14:47
  • Are you trying for fetch query result or insert?
    – S.P
    Commented Jan 19, 2023 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.