upload file to table

upload file to table

AloneInTheDarkAloneInTheDark Posts: 30Questions: 7Answers: 0
edited May 2021 in Free community support

Hi
i have this table:

**DB TABLE: edoc_document_request **:

id_edoc (pk)
file_durc (varchar(250)

JS:

table_user_edit =  new $.fn.dataTable.Editor( {
            ajax:urlService_list_edit_post,
            table: "#tbl-ordini-edit",
            fields: [ 

               {
                    label: "ID",
                    name: "document_request.id_edoc",
                    type: "readonly"
     
                }
                ,{
                    label: "DURC:",
                    name: "document_request.file_durc",
                    type: "upload",
                    display: function ( data, type,row ) {
                        return 'DURC caricato';
                    },
                    clearText: "Clear",
                    noImageText: 'No DURC'
                }        

PHP:

$editor = Editor::inst( $db, 'edoc_document_request as document_request', 'id_edoc' )
->fields(
    Field::inst( 'document_request.id_edoc' ),
    Field::inst( 'document_request.id_user' ),
...
    Field::inst( 'document_request.file_durc' )
        ->setFormatter( Format::ifEmpty( null ) )
        ->upload(
            Upload::inst( function ( $file, $id ) use ( $db ) {
                $extn = pathinfo( $file['name'], PATHINFO_EXTENSION ); // get extension

                //$id is null
                $webPath = '/durc_'.$id.$extn;
                

                $serverPath = $_SERVER['DOCUMENT_ROOT'] . $webPath;
    
                // Save the file on the server
                move_uploaded_file( $file['tmp_name'], $serverPath );
    
                // Set the `url` in the database
                $db->update('edoc_document_request',
                    array(
                        'file_durc' => $webPath
                    ),
                    array( 'id_edoc' => $id  )
                );
    
                return $id;
            } )
        ->validator( ... )
    )
...
)

->leftJoin( ... )
->debug(true)
->process($_POST)
->json();

Problem
This code adds a new record and does not update the existing , because $id is NULL
how can i pass the my row id? Help please.
Tnk

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi,

    Unfortunately this isn't how the Editor upload is designed to operate. The use case for our file upload is to have a host table which is linked to a table of files. The file upload is async to the rest of the form, so the main form cannot be used to edit the files table I'm afraid.

    The closest to the above would be to have a second table which is id and fileId where fileId points to the table above.

    Regards,
    Allan

This discussion has been closed.