string(3) "ERP" ["SOURCE"]=> -6ren">
gpt4 book ai didi

php - 从 postgresql 函数参数中检索 php 数组键和值以进行数据库更新

转载 作者:可可西里 更新时间:2023-10-31 23:38:57 24 4
gpt4 key购买 nike

我正在尝试将一组键值对作为参数发送到 postgresql 函数。数组的结构如下 -

array(10) { 
["OWNER"]=> string(3) "ERP"
["SOURCE"]=> string(7) "Unknown"
["PRIORITY"]=> string(6) "Medium"
["PREFLOC"]=> string(5) "Dhaka"
["PROBABLE"]=> string(2) "50"
["MAXSIZE"]=> string(4) "1000"
["MINSIZE"]=> string(4) "2000"
["INTAREA"]=> string(14) "Dhaka, Gulshan"
["CVALPRF"]=> string(5) "Great"
["OPPAMOUNT"]=> string(3) "200"
}

函数接受这样的字符串数组参数

CREATE OR REPLACE FUNCTION 
document.update_doc_attrib_on_opportunity(p_org_id numeric, p_target_doc_code character varying,
p_target_doc_no numeric, p_doc_attribs character varying[])

现在我想在我的函数中将数组发送到 p_doc_attribs。就像特定的键名一样,我需要在表中插入所需的值。以下查询需要相应更新 -

'UPDATE use_doc_attribute
SET attrib_value = CASE WHEN attrib_code = ''PREFLOC'' THEN ''' || p_preferred_location || '''
WHEN attrib_code = ''PRIORITY'' THEN ''' || p_priority || '''
WHEN attrib_code = ''PROBABLE'' THEN ''' || p_probability || '''
WHEN attrib_code = ''SOURCE'' THEN ''' || p_source || '''
WHEN attrib_code = ''MAXSIZE'' THEN ''' || p_max_size || '''
WHEN attrib_code = ''MINSIZE'' THEN ''' || p_min_size || '''
WHEN attrib_code = ''INTAREA'' THEN ''' || p_interested_areas || '''
WHEN attrib_code = ''CVALPRF'' THEN ''' || p_client_value_profile || '''
ELSE attrib_value
END
WHERE org_id = ' || p_org_id || '
AND document_no = ' || p_target_doc_no || '
AND document_code = ''' || p_target_doc_code || '''';

attrib_code 将包含键,特定情况下的 attrib_value 将是从 p_doc_attribs 数组中检索到的值。

最佳答案

我不是这方面的专家,但您可以json_encode 数组并将其传递给您的函数,为此您必须对您的函数进行以下更改:

CREATE OR REPLACE FUNCTION document.update_doc_attrib_on_opportunity(
p_org_id numeric,
p_target_doc_code character varying,
p_target_doc_no numeric,
p_doc_attribs JSON)

您的查询可能是这样的:

'UPDATE use_doc_attribute
SET attrib_value = CASE WHEN attrib_code = ''PREFLOC'' THEN ''' || p_doc_attribs['PREFLOC'] || '''
WHEN attrib_code = ''PRIORITY'' THEN ''' || p_doc_attribs['PRIORITY'] || '''
WHEN attrib_code = ''PROBABLE'' THEN ''' || p_doc_attribs['PROBABLE'] || '''
WHEN attrib_code = ''SOURCE'' THEN ''' || p_doc_attribs['SOURCE'] || '''
WHEN attrib_code = ''MAXSIZE'' THEN ''' || p_doc_attribs['MAXSIZE'] || '''
WHEN attrib_code = ''MINSIZE'' THEN ''' || p_doc_attribs['MINSIZE'] || '''
WHEN attrib_code = ''INTAREA'' THEN ''' || p_doc_attribs['INTAREA']|| '''
WHEN attrib_code = ''CVALPRF'' THEN ''' || p_doc_attribs['CVALPRF'] || '''
ELSE attrib_value
END
WHERE org_id = ' || p_org_id || '
AND document_no = ' || p_target_doc_no || '
AND document_code = ''' || p_target_doc_code || '''';

您可以查看以下link更多这样的例子。

编辑

我认为要使其正常工作,您需要安装 PLV8 扩展,您可以找到更多信息 here

为 V 9.1.x 编辑

例如,您可以创建并传递索引数组,创建类似于以下的数组:

[
"ERP",
"Unknown",
"Medium",
"Dhaka",
"50",
"1000",
"2000",
"Dhaka, Gulshan",
"Great",
"200"
]

然后使用 PHP array to postgres array函数将该数组转换并传递给 postgresql 函数,并将查询中使用的数组值更改为基于索引而不是基于键,如下所示:

p_doc_attribs[3]

代替

p_doc_attribs['PREFLOC']

关于php - 从 postgresql 函数参数中检索 php 数组键和值以进行数据库更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32579038/

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