Editor Upload With Custom Primary Key

Editor Upload With Custom Primary Key

HassanDomeDeneaHassanDomeDenea Posts: 29Questions: 10Answers: 0

Greetings, I use certain table to store files paths.
But I use special primary key (key_id).
Although I've defined key_id in the editor, it still using the ID in file naming and referencing.
Here is my code:

            Upload::inst($_SERVER['DOCUMENT_ROOT'] . '/uploads/__ID__.__EXTN__')
                ->db('files_list', 'key_id', [
                    'key_id'=> getSpecialID() // Return unique primary key
                    'name' => Upload::DB_FILE_NAME,
                    'file_size' => Upload::DB_FILE_SIZE,
                    'file_type' => Upload::DB_CONTENT_TYPE,
                    'path' => Upload::DB_WEB_PATH,
                    'realpath' => Upload::DB_SYSTEM_PATH,
                ])
                ->dbClean(function ($data) {
                    // Remove the files from the file system
                    for ($i = 0, $ien = count($data); $i < $ien; $i++) {
                        unlink($data[$i]['realpath']);
                    }
                    // Have Editor remove the rows from the database
                    return true;
                })          

I find my files in the desirder location, but named with the table id column. Also the reference in the main table, it is referencing to ID.
Did I missed something ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The libraries use the insert id which I think explains this. I'm assuming you are using MySQL? The driver for that always uses the id returned by the database.

    I need to have a bit of a think about the right way to handle this, but at the moment, rather than using the default upload action with a string, you could use your own to move it into the location required.

    Allan

  • HassanDomeDeneaHassanDomeDenea Posts: 29Questions: 10Answers: 0

    I see.
    The solution you mentioned is fine, but I think the move_uploaded_file method is running before database row insertion, thus I can't access the the $id of the inserted row before insertion, or does it (the upload function) run after database insertion ?
    Anyway just was wondering, I think I just leave the id of file for now.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The db execute runs before the custom action function so you should have access to the database id in the action callback.

    Allan

This discussion has been closed.