Field 'filename' on file upload

Field 'filename' on file upload

djmm68djmm68 Posts: 20Questions: 7Answers: 0

My question is about uploading files in Editor. I've been reading the manual and according to it, on server side I have:

...
        Field::inst( 'image_or_document_id' )
            ->setFormatter( 'Format::ifEmpty', null )
            ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )
                ->db( 'image_table', 'id', array(
                    'filename'    => Upload::DB_FILE_NAME,
                    'filesize'    => Upload::DB_FILE_SIZE,
                    'web_path'    => Upload::DB_WEB_PATH,
                    'system_path' => Upload::DB_SYSTEM_PATH
                ) )

Then, the image or document (p.e. pdf) id is stored in table 'image_table'. So far so good. But now, I need to create a new field on main table (the one to which 'image_or_document_id' field belongs), a field called 'filename_with_extension', and I'd like it to be auto-populated with the same value that is going to be inserted on table 'image_table' at the moment of file attachment (when I complete the upload operation), concretely: field 'filename'. In other words, the goal is getting this value repeated, twice:

1) Table 'image_table', field 'filename'
2) Main table, field 'filename_with_extension'

How can I do this? Do I need 'preSubmit' event for this, or it can be done only with php events or configuring 'setValue' parameter?

Thanks in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Answer ✓

    What I would suggest you do is use a left join to pull in the information about the file based on the primary key join.

    The Upload::DB_FILE_NAME should actually have the extension on it, so you just need to left join to the filename column in your image_table table and then have DataTables display that field.

    ->Field::inst( 'image_table.filename' )
    ->leftJoin( 'image_table', 'image_table.id', 'hostTableName.primaryKeyName' )
    

    And in the DataTable columns array:

    { data: 'image_table.filename' }
    

    Allan

  • djmm68djmm68 Posts: 20Questions: 7Answers: 0

    Great! Left Join!! That solved the issue.
    Thanks Allan!

This discussion has been closed.