xmlyii2active-form

Is there anyway to convert XML into an Activeform/Database? (Yii2)


I'm still pretty new to Yii2 and was trying to find out if it's possible to load an XML file into an activeform that can then be uploaded to a database and that data then is viewable to anyone with the right access permissions.

I've looked around online and seen that you can load an XML file but display it as raw data, I've looked at these but I cannot figure out how I'd go about converting it into active-form.

Edit: Or if it's possible to upload the XML file to the database directly will work.


Solution

  • $model = new XMLFileUpload();
        $testupload = new test();
        if (Yii::$app->request->isPost)
        {
            $model->file = UploadedFile::getInstance($model, 'file');
    
            if ($model->file && $model->validate())
            {
                $model->file->saveAs('uploads/' .$model->file->baseName . '.' . $model->file->extension);
                $xmlfile = file_get_contents('uploads/' .$model->file->baseName . '.' . $model->file->extension);
                $xmlob = simplexml_load_string($xmlfile);
                $xmljson = json_encode($xmlob);
                $xmlarray = json_decode($xmljson, true);
                $testupload->attributes = $xmlarray;
                $testupload->save(false);
            }
        }