is it possible to use uploadMany without Join ?

is it possible to use uploadMany without Join ?

yokowasisyokowasis Posts: 24Questions: 7Answers: 0
edited April 2020 in Free community support

I can't really make it works. It just keep throwing "Array to string conversion in MysqlQuery.php"

The example is using join table, is it possible to not doing this ?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    I guess you are referring to this example:
    https://editor.datatables.net/examples/advanced/upload-many

    Well, you definitely need the join!

    What you need is:
    - a table holding the references to your files ("file")
    - a table holding the logical entity you are saving files for (e.g. "widget", "contract", "tool" whatever it is ...) - and that is the table you are actually editing!
    - the link table between the two

    Take a look at this thread please:
    https://datatables.net/forums/discussion/comment/164076/

  • yokowasisyokowasis Posts: 24Questions: 7Answers: 0

    Yes, that's what I am referring to.

    I mean it's possible to use single upload without join. Can't we just store the the id of the files separated by comma in the database ?

  • yokowasisyokowasis Posts: 24Questions: 7Answers: 0

    How about this, is there a way to catch the multi uploaded files ids, and submit that instead

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Well, you can do anything. But you would need to code it yourself, I guess. Why reinvent something that already exists and works? And it certainly is better data modelling to use the 3 tables as discussed instead of saving a comma separated string with ids ... But that is up to you of course :smiley:

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin

    To confirm my understanding @yokowasis, you want to allow multiple files to be uploaded for a single field, but just to store the ids for the files in a comma separated string (or something) in the host table, rather than needing to use a link / junction table?

    Personally I would suggest against doing it that way - use a link table with Mjoin, but if you must do it with a comma separated list of ids, you could use the array formatters on the server-side to chop it into an array (getFormatter) and bring the array from the client-side back into a string on set.

    Allan

  • yokowasisyokowasis Posts: 24Questions: 7Answers: 0
    edited April 2020

    @allan

    Yes that's what I want. I don't know why the method would be different between single and multiple uploads.

    In single uploads you just need 1 additional table (table files), contain information such as url, server path, etc. And that's all you need. No join needed.

    I tried it your suggestion, but it doesn't work. Any example would be great.

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin

    Could you show me what you used please?

    Thanks,
    Allan

  • yokowasisyokowasis Posts: 24Questions: 7Answers: 0

    For future user who has the same problem with me, here is the code that works for me

                            $Field
                                ->getFormatter(Format::explode())
                                ->setFormatter(Format::implode())
                                ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/'.$this->uploadfolder.'/__ID__.__EXTN__' )
                                    ->db( 'files', 'id', array(
                                        'filename'    => Upload::DB_FILE_NAME,
                                        'filesize'    => Upload::DB_FILE_SIZE,
                                        'web_path'    => Upload::DB_WEB_PATH,
                                        'system_path' => Upload::DB_SYSTEM_PATH
                                    ) )                        
                                    ->validator( Validate::fileSize( 5000000, 'Maksimal File 5MB' ) )
                                    ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif', 'pdf', 'docx', 'doc', 'csv', 'xlsx', 'xls' ), "File tidak dikenali" ) )
                                );
    

    Thanks @allan for the help.

    I have no idea why it didn't work before. I delete it, because, well, it didn't work.

  • yokowasisyokowasis Posts: 24Questions: 7Answers: 0
    edited April 2020

    Ok, now another problem arise.

    I can save and retrieve, but I can't edit.

    It says

    dataTables.editor.min.js:12 Uncaught Unknown file id 17 in table files
    

    here is the table json

    {
        "data": [{
            "DT_RowId": "row_9",
            "id": "9",
            "checkbox": "",
            "date": "2020-04-17",
            "datetime": "2020-04-20 08:06:08",
            "hidden": "Default Value",
            "password": "asdasdasd",
            "radio": "",
            "readonly": "ReadOnly Value",
            "select": "-",
            "text": "",
            "textarea": "",
            "upload": "1",
            "uploadMany": ["12", "17"],
            "uploadManyID": "3",
            "image": null,
            "currency": "",
            "autocomplete": "",
            "multiselect": "",
            "selectdb": "-",
            "rtf": ""
        }],
        "options": [],
        "files": {
            "files": {
                "1": {
                    "id": "1",
                    "filename": "LOGO diginusa.jpg",
                    "filesize": "1207774",
                    "web_path": "\/bimasoft-wp-mms\/wp-content\/uploads\/1.jpg",
                    "system_path": "\/home\/u0328216\/aplikasi.mtsn1kobi.sch.id\/bimasoft-wp-mms\/wp-content\/uploads\/1.jpg"
                },
                "12": {
                    "id": "12",
                    "filename": "asd.jpg",
                    "filesize": "36280",
                    "web_path": "\/bimasoft-wp-mms\/wp-content\/uploads\/12.jpg",
                    "system_path": "\/Volumes\/Data\/git\/bimasoft-wp-mms\/wp-content\/uploads\/12.jpg"
                }
            }
        }
    }
    

    Probably because there is no id 17 on the files. It seems on upload many, only the first id, is included in the files json.

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Answer ✓

    What version of the server-side libraries are you using please? It does appear to work in this example which is using 1.9.2.

    Thanks,
    Allan

  • yokowasisyokowasis Posts: 24Questions: 7Answers: 0

    Okay, I got it to work

                            $Field
                                ->getFormatter(
                                    function ( $val, $data ) {
                                        if ($val)
                                        return explode("|", $val);
                                        else return null;
                                    }                                
                                );
                            $Field->setFormatter(
                                function ( $val, $data ) {
                                    if ($val)
                                        return implode("|", $val);
                                    else return null;
                                }                    
                            );
    
                            $Field
                                ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/'.$this->uploadfolder.'/__ID__.__EXTN__' )
                                    ->db( 'files', 'id', array(
                                        'filename'    => Upload::DB_FILE_NAME,
                                        'filesize'    => Upload::DB_FILE_SIZE,
                                        'web_path'    => Upload::DB_WEB_PATH,
                                        'system_path' => Upload::DB_SYSTEM_PATH
                                    ) )                        
                                    ->validator( Validate::fileSize( 5000000, 'Maksimal File 5MB' ) )
                                    ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif', 'pdf', 'docx', 'doc', 'csv', 'xlsx', 'xls' ), "File type salah" ) )
                                );
    
    

    Upload Many, no need to use join.

This discussion has been closed.