gpt4 book ai didi

mysql - 在 Magento 中为模块创建数据库函数

转载 作者:行者123 更新时间:2023-11-29 02:32:06 24 4
gpt4 key购买 nike

我在 Magento 中有一个工作模块,它是根据我们在安装之外使用的一些自定义代码建模的。该模块目前向数据库添加了 5 个表来存储信息,我已经将 Admin 扩展到 CRUD 信息。这里的最终目标是将大部分自定义编程转移到 Magento 中。

目前,我们的自定义代码位于 Magento 外部并访问一个单独的数据库。该数据库具有相同的 5 个表、一个存储过程和 4 个函数。我现在想做的是将存储过程和函数移动到 Magento 的数据库中,并更改自定义代码以从 Magento 的数据库中调用所有数据。但是,我似乎无法弄清楚应该如何设置“CREATE FUNCTION”调用以便 Magento 正确执行它。

我使用的 SQL 是:

DROP FUNCTION IF EXISTS {$this->getTable('fn_Get_HardinessZone')};
CREATE FUNCTION {$this->getTable('fn_Get_HardinessZone')}(IN ZipCode varchar()) RETURNS integer AS
DECLARE Result integer;
BEGIN
SELECT MAX(Zone) into Result
FROM AMI_zones
WHERE (Hfzip <= LEFT(ZipCode, 5)) AND (Htzip >= LEFT(ZipCode, 5));
if Result is null or Result < 1 or (Result > 11 and Result <> 99) Then
/*if the left most character is alpha, then set the zone to 98 for Canada*/
if Left(zipCode, 1) >= 'A' and LEFT(zipcode,1) <= 'Z' THEN
set result = 98;
else
set Result = 99;
End if;
END if;
RETURN Result;
END;

但这总是会产生以下错误:

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 'IN ZipCode varchar()) RETURNS'

那么格式化 SQL 调用以在模块的安装/更新脚本中运行以将函数或存储过程插入 Maganeto 的数据库的正确方法是什么?

最佳答案

问题出在你的 SQL 语句上:

You have an error in your SQL syntax; check the manual ... for the right syntax to use near

'IN ZipCode varchar()) RETURNS'

我建议通过 PhpMyAdmin 或命令行运行 SQL,直到正确为止,然后通过 Magento 运行它。此手册页描述了 CREATE FUNCTION 的语法:http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html .在 mysql 客户端(或 PhpMyAdmin)中测试存储过程/函数时,确保 change the delimiter以便正确解释函数体中的分号。

下面的 SQL 对我有用。我从您的原始陈述中更改的内容是:

  1. IN 不允许出现在函数声明中 (IN ZipCode varchar())
  2. 我被要求明确说明 varchar 的长度
  3. DECLARE 属于函数内部
  4. 我猜您的函数是 DETERMINISTIC,这意味着对于相同的输入参数,它将始终产生相同的结果。如果不是这种情况,请从 RETURNS 行中删除 DETERMINISTIC

试一试:

DROP FUNCTION IF EXISTS {$this->getTable('fn_Get_HardinessZone')};
CREATE FUNCTION {$this->getTable('fn_Get_HardinessZone')} (ZipCode VARCHAR(15))
RETURNS INTEGER DETERMINISTIC
BEGIN
DECLARE result INTEGER;
SELECT MAX(Zone) INTO result
FROM AMI_zones
WHERE (Hfzip <= LEFT(ZipCode, 5)) AND (Htzip >= LEFT(ZipCode, 5));
IF result IS NULL OR result < 1 OR (result > 11 AND result <> 99) THEN
/* if the left most character is alpha, then set the zone to 98 for Canada */
IF LEFT(ZipCode, 1) >= 'A' AND LEFT(ZipCode, 1) <= 'Z' THEN
SET result = 98;
ELSE
SET result = 99;
END IF;
END IF;
RETURN result;
END;

关于mysql - 在 Magento 中为模块创建数据库函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11586096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com