【发布时间】:2012-08-16 08:02:34
【问题描述】:
我尝试将 PHP 数组传递给以下函数,但收效甚微。
$recipients = array();
$recipients['6c2f7451-bac8-4cdd-87ce-45d55d152078'] = 5.00;
$recipients['8c2f3421-bac8-4cdd-87ce-45d55d152074'] = 10.00;
$originator = '5630cc6d-f994-43ff-abec-1ebb74d39a3f';
$params = array($originator, $recipients);
pg_query_params(_conn, 'SELECT widgetallocationrequest($1, $2)', $params);
$results = pg_fetch_all(pg_query_params);
...
该函数接受一个自定义类型的数组:
CREATE TYPE widgetallocationrequest AS (id uuid, amount numeric(13,4));
枚举每个项目并执行一个动作:
CREATE FUNCTION distributeWidgets(pid uuid, precipients widgetallocationrequest[])
RETURNS BOOLEAN AS
$BODY$
{
FOREACH v_recipient IN ARRAY precipients
LOOP
...do something
END LOOP;
}
RETURN TRUE;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT
COST 100;
***(if the specific code sample contains errors it's only pseudocode, i'm really just looking for the best way to pass a php array to a postgres custom type array as a parameter so it can be enumerated in the postgres function)***
更新: 我可以使用以下命令直接从 postgres(而不是 PHP)成功调用该函数:
SELECT distributeWidgets('5630cc6d-f994-43ff-abec-1ebb74d39a3f',
ARRAY[('ac747f0e-93d4-43a9-bc5b-09df06593239', '5.00'), ('8c2f3421-bac8-4cdd-87ce-45d55d152074', '10.00')]::widgetallocationrequest[]);
但仍然不确定如何从这个 postgres 示例转换回 PHP
我尝试了以下建议,引用函数的输出产生以下错误:
函数的字符串如下:
'SELECT account.hldtoexpalloc('0d6311cc-0d74-4a32-8cf9-87835651e1ee', '0124a045-b2e8-4a9f-b8c4-43b1e4cf638d', '{{\"6c2f7451-bac8-4cdd-87ce-45d55d152078\",5.00},{\"8c2f3421-bac8-4cdd-87ce-45d55d152074\",10.00}}')'
【问题讨论】:
-
到目前为止您尝试过什么?错误信息?
-
code Query failed: ERROR: array value must start with "{" or dimension information in -
我从错误中收集到,这不是对自定义类型的直接数组转换支持,这是否必须是手动字符串构建,或者是否有元类可用于将数组映射到 postgres 自定义输入?
-
你不必在
pg_query_params的参数中写$recipients吗?我不会说 PHP,所以这可能完全不合时宜。如果 PHP 的 Pg 支持不支持简单数组,我会感到有点惊讶。另外,$params是干什么用的?它似乎没有使用。 -
很抱歉,$params 应该是传递给 pg_query_params 函数的参数数组,在该对象内部是我试图传递的自定义类型数组。
标签: php postgresql postgresql-9.1