phpweb-servicesmoodlemoodle-mobile

Moodle Use Webservice for store data


I'm currently trying to write data from the app into the database. I'm using the web service for this.

However, I get the following error message and the documentation is, in my opinion, very sparse.

core_external\external_multiple_structure::__ construct(): Argument #1 ($Content) must be of type core_external\external_description, array given, called in

The relevant line is the following:

'eintraege' => new external_multiple_structure([

The complete function is:

public static function execute_returns(): external_single_structure {
        return new external_single_structure([
            'eintraege' => new external_multiple_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'),
            ])
        ]);
    }

I would be very grateful for any help and thank you very much for that!

I tried the following variations: `A parameter can be described as:

a list => external_multiple_structure an object => external_single_structure a primary type => external_value`

As described here: https://moodledev.io/docs/4.5/apis/subsystems/external/writing-a-service


Solution

  • Looking at the docs you provided, I can see that you're passing the parameters wrong.

    The following line in your case:

    'eintraege' => new external_multiple_structure([
       'fach' => new external_value(PARAM_INT, 'FachID'),
       //etc
    ])
    

    should return a new external_single_structure instance as well, but in your case, it's just returning the array. Try to make it like this:

    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'),
                ])
            )
        ]);
    }