【问题标题】:Magento: Stored Procedures in SQL Upgrade ScriptsMagento:SQL 升级脚本中的存储过程
【发布时间】:2011-10-22 10:39:48
【问题描述】:

我一直在尝试将存储过程创建步骤放入我在 Magento 中的 SQL 升级脚本中。 喜欢

$this->startSetup();
$sql = <<< __SQLPRC
CREATE PROCEDURE my_proc(
    IN length int
    ,IN column_used_for varchar(50)
    ,OUT return_id bigint
)
BEGIN
    DECLARE count_unused INT;
    SET count_unused = 0;
    select 
            id into return_id
        from 
            my_table
        where 
            used_status=0
            limit 1;

    if length(return_id) = length 
    then
        update my_table
            set 
                used_status=1
            where 
                id = return_id;
    else
        set return_id = 0;
    end if;
END;
__SQLPRC;

try {
$this->run($sql);
}
catch(Exception $e)
{ echo "<pre>" . $e->getTraceAsString(); }

$this->endSetup();

我从调试中得出的结论是,Magento 只抓取 SQL 直到第一个分号“;” 有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: php stored-procedures magento


    【解决方案1】:

    您已将存储过程代码设置为:-

    $this->startSetup();
    $sql = <<< __SQLPRC
    ...
    

    这需要是:-

    $this->startSetup();
    $sql = <<<__SQLPRC
    ...
    

    现在应该可以了,因为根据 PHP 字符串分隔的 Heredoc Syntax,“&lt;&lt;&lt;”之后不应有任何空格。

    希望对你有帮助。


    更新答案:-

    您可以尝试使用此代码吗:-

    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    $write->exec($sql);
    

    而不是:-

    $this->run($sql);
    

    我之前在 Magento 上看到过一些与此相关的问题。

    【讨论】:

    • 不,仍然是同样的问题:(在第一个分号之后它仍然不考虑任何内容。
    • 只是想注意,您可以为模块 SQL 安装指定 .sql 脚本而不是 .php。看看 app/code/core/Mage/Core/Model/Resource/Setup.php _modifyResourceDb
    • 即使使用 SQL 文件,它也只会考虑查询到第一个分号。
    • 进一步调试,我发现问题在于 lib/Varien/Db/Adapter/Pdo/Mysql.php 中的 _splitMultiQuery() 函数按以下顺序调用 run() 调用 multi_query()调用 _splitMultiQuery() 此函数根据分号拆分查询并单独执行每个部分。还有什么建议吗?
    • @Knowledge-Craving:非常感谢。使用 exec() 代替 run() 有效。
    猜你喜欢
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多