我试图将 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}}')'