使用Moodle WebService存储数据遇external_multiple_structure参数错误求助
解决Moodle WebService返回结构报错:external_multiple_structure参数类型错误
问题分析
报错信息明确指出:core_external\external_multiple_structure::__construct(): Argument #1 ($Content) must be of type core_external\external_description, array given。这是因为external_multiple_structure的构造函数要求传入的是单个结构描述(external_description的子类,比如external_single_structure),而非直接传入字段数组。
你的代码里直接把字段数组传给了external_multiple_structure,不符合Moodle外部API的类型规范。
修正后的代码
public static function execute_returns(): external_single_structure { return new external_single_structure([ 'eintraege' => new external_multiple_structure( new external_single_structure([ 'fach' => new external_value(PARAM_INT, 'FachID'), 'datum' => new external_value(PARAM_RAW, 'Datum des Eintrags'), 'ustdbis' => new external_value(PARAM_INT, 'USTD bis'), 'ustdvon' => new external_value(PARAM_INT, 'USTD von'), 'eintrag' => new external_value(PARAM_TEXT, 'Eintrag'), ]) ) ]); }
关键说明
external_multiple_structure用于描述一组相同结构的数据集合,它的参数必须是一个external_single_structure(定义集合内每个元素的具体结构),不能直接传递字段数组。- 你原本的结构逻辑方向是对的:列表用
external_multiple_structure、对象用external_single_structure、基础类型用external_value,只是漏掉了将字段数组用external_single_structure包裹后,再传给external_multiple_structure的步骤。
内容的提问来源于stack exchange,提问作者Sascha
相关产品推荐
相关产品推荐

