File upload (many)

File upload (many)

Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0

Hi , I am using this example to upload multiple files to the db and it works perfect.
https://editor.datatables.net/examples/advanced/upload-many.html

In the serverside script code, how can I pass the field value for example Field::inst( 'users.first_name' ), which can be saved in the table files on database. something like this:

->join(
        Mjoin::inst( 'files' )
            ->link( 'users.id', 'users_files.user_id' )
            ->link( 'files.id', 'users_files.file_id' )
            ->fields(
                Field::inst( 'id' )
                    ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__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,
                            'user_name' => Field::inst( 'users.first_name' )

                        ) )
                        ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
                        ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                    )
            )
    )

Thank you

Answers

  • allanallan Posts: 61,611Questions: 1Answers: 10,089 Site admin

    The problem with doing that is, what if the user then changed the value of the users.first_name field after they'd done the upload? Your files table would now contain old and incorrect information.

    Since it is a join, can you not just include the users.first_name field in the regular field list? That way to maintain referential integrity and don't duplicate information in the database.

    Allan

  • Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0

    @allan thank you. Yes, you are absolutely right. In this way referential integrity wont be maintained.

    What I was trying to achieve is , access the row data (row that is been edited) in server side script before writing it to database, using:

    if ( isset( $postData['action'] ) && ( $postData['action'] === 'create' || $postData['action'] === 'edit' ) ) {
    
             $variable =  $postData['data']['row_205549']['products']['supplier_id'];
    }
    

    After I receive the data , I will use the supplier_id from the products table and write it to table files using:

         ->join(
                 Mjoin::inst( 'files' )
                     ->link( 'crg_products.product_id', 'users_files.supplier_id' )
                     ->link( 'files.id', 'users_files.file_id' )
                     ->fields(
                         Field::inst( 'id' )
                             ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__ID__.__NAME__' )
                                 ->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,
                                     'supplier_id' =>  $variable
                                 ) )
                             ))      
             );
    

    1) Somehow it doesn't work :(

    The reason for that is when I edit a particular row in products table, I want to take the supplier id from that row and save it in** files table**. So I can link all the products in table with same supplier id to the files with are saved in files table.

    2) How can I access the row data on server side without using the row id

       Currently I access it using: 
          $variable =  $postData['data']['row_205549']['products']['supplier_id'];
    

    Because every time the row is edited , the row id is changed. so is there a way to get the edited row data without specifying the row id?

    for example to access the edited row data, can we use:

                 $variable =  $postData['data']['products']['supplier_id'];
    

    Thank you
    As always appreciate your help

  • allanallan Posts: 61,611Questions: 1Answers: 10,089 Site admin

    What I was trying to achieve is , access the row data (row that is been edited) in server side script before writing it to database, using:

    But what if the user changes the value after submission? Or if they haven't entered it yet? For a file upload, I'd say you should try to avoid any of the other fields in the form - only on reading the data back should it be recombined with the joined values to make sure that everything is synced up.

    Allan

  • Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0

    Hi @allan
    Thanks for making me understand. I have sorted it out now.

    I'd say you should try to avoid any of the other fields in the form - only on reading the data back should it be recombined with the joined values to make sure that everything is synced up.

    This is definitely the best practice.

This discussion has been closed.