File upload (many)
File upload (many)
Khalid 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
This discussion has been closed.
Answers
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? Yourfiles
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
@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:
After I receive the data , I will use the
supplier_id
from theproducts table
and write it totable files
using: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
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:
Thank you
As always appreciate your help
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
Hi @allan
Thanks for making me understand. I have sorted it out now.
This is definitely the best practice.