简体   繁体   中英

How to add many elements to database with MODX-processor

I have a file-uploading-processor with array output. How I can add this array to database?

That's my processor's code:

class ModCLJsonUploadProcessor extends modProcessor {
    public $languageTopics = ['modcl'];

    public function process() {
        $file = fopen($_FILES['json-file']['tmp_name'], 'r');
        $json = fread($file, $_FILES['json-file']['size']);
        $objs = json_decode($json);
        $english = array();
        for ($i = 0; $i < count($objs); $i++) {
            $english[$i] = $objs[$i]->{'name'};
        }

        return $this->success($english);
    }
}

return 'ModCLJsonUploadProcessor';

I tried to use the native modObjectCreateProcessor, but it does not support arrays.

In order to save items into the database using a processor, the best practice would be to create a schema and save the given data as objects in the database. This allows you to then later retreive the items (objects) in other processors or snippets using xPDO.

For a basic explenation/how to on creating a schema and interacting with the custom objects have a look at the "Developing an extra in MODX Revolution" doc here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM