How to use a just created ID in another field, upon save

edited February 2013 in Modules
The answer is probably obvious, but I'm running in around in circles here. I have an IMAGES table with and image_id (primary key) field and an image_filename field.

I would like to integrate the value that will be stored into the image_id field into the filename I will store in the image_filename field (ex: flower22 where 22 is the image_id) when I first CREATE the entry.

Can I use the image_id created in other fields during that first save? How?

Comments

  • edited 5:44PM
    What version of FUEL? If 1.0 beta are you using the 'asset' field type or the 'file' field type to upload the image?
  • edited February 2013
    1.0 beta -- 'file' field type.
  • edited 5:44PM
    I'm sorry, I don't want to overly confuse the issue. Yes, I'm working with image uploads but I would like to understand the principle so I could to the same thing with a simple PRODUCTS table with a 'products_id' field and a 'product_filename' field.
  • edited 5:44PM
    In other words, I would like to be able to insert the value of an ID field into any other field upon first creating the entry (first save) when the ID is actually created.
  • edited 5:44PM
    The current implementation requires the use of an on_after_post hook to rename the uploaded file. However, I'm working on an easier solution where you would simply just need to pass in place holders in the file_name parameter like so:
    $fields['file'] = array('type' => 'file', 'file_name' => 'test_{id}');
    I'm hoping to have it tested and ready tomorrow.
  • edited 5:44PM
    That's great news!

    I was indeed already renaming the file in an on_after_post hook. I also put together some logic in an on_after_save hook to update some fields with the ID created during the (first) save. Your placeholders would, I think, greatly simplify this process.
  • edited 5:44PM
    I've pushed some changes to the 1.0 branch to allow you to add placed holders to the file name. If you are wanting to customize the file name and need a field for that which will by default be the generated file name, I'd recommend doing something like the following:
    $fields['file'] = array(); $fields['file_upload'] = array('type' => 'file', 'file_name' => 'test_{id}');
    Name a field name with a suffix of "_upload" will automatically transfer the name of the file to that field's value.
  • edited 5:44PM
    I've dowloaded the latest files from 1.0 branch.

    Yes, adding the '_upload' suffix does transfer the filename to that field's value. However, using the 'file_name' parameter does not change the transfered filename.

    So even if I've set 'file_name' to 'test_{id}', and the name of the file being uploaded is "orange.txt", I'm still getting "orange.txt" transfered to the field and saved to the database.
  • edited 5:44PM
    Hmm... I'm not seeing that locally. You mind posting the whole content of your model? And just to confirm, you don't have anything in a hook that may be changing that? The processing to save the name of the file should be happening around line 1995 of the fuel/modules/fuel/controllers/module.php file.
  • edited 5:44PM
    I'm having this same problem, I set the file_name to 'test_{id}' and when I save the record it correctly sets the filename to 'test_1' but the actual file in the assets folder has been saved as 'test_{id}.png'. I am using version 1.0 and the asset field type.
  • edited 5:44PM
    The "asset" field type or the "file" field type? The inserting of the "id" will only work for a "file" field type since the asset field type does it's uploading in a separate window.
Sign In or Register to comment.