How to add multiple image uploads and connecting two models

edited June 2012 in News & Announcements
Hi,

I have a table called members and a table called member_images.
The member_images table has these fields: id, member_id, image
The members table has these fields: id, title, address, phone, site, logo, date_added, content, published.

Each record in the members table can have multiple records in member_images table.

What I would like to do:
When the user creates a member on the admin side, he will have the option to upload as many images as he wants to the member he is creating.

I can't get my head around on how to do that.
First step I created the member_images_model:
class Member_images_model extends Base_module_model { public $record_class = 'Member_image'; public $foreign_keys = array('member_id' => 'members_model'); function __construct() { parent::__construct('member_images'); } function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc') { $this->db->join('members', 'members.id = member_images.member_id', 'left'); $this->db->select('member_images.id, members.title AS member, image', FALSE); $data = parent::list_items($limit, $offset, $col, $order); return $data; } // cleanup images to member function on_after_delete($where) { $this->delete_related('member_images', 'member_id', $where); } } class Member_image_model extends Base_module_record { }

Then in my members model I added this:
function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('categories_to_members_model'); $CI->load->model('member_images_model'); $category_options = $CI->categories_model->options_list('id', 'name', array('published' => 'yes'), 'name'); $category_values = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('category_id', array('member_id' => $values['id'], 'categories.published' => 'yes'))) : array(); $fields['categories'] = array('label' => 'Categories', 'type' => 'array', 'class' => 'add_edit categories', 'options' => $category_options, 'value' => $category_values, 'mode' => 'multi'); $upload_path = assets_server_path('members/', 'images'); $fields['logo_upload'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE); $fields['image'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE); $fields['add_another_image'] = array('type' => 'button', 'value' => 'Add', 'use_input' => FALSE); return $fields; } function on_after_save($values) { $data = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $data)); $data = (!empty($this->normalized_save_data['image'])) ? $this->normalized_save_data['image'] : array(); $this->save_related('member_images_model', array('member_id' => $values['id']), array('image' => $data)); }

Now I have a few questions:

1.
When I create a member and upload an image in the image field, it is not being saved into the member_images table.
Why not?

2.
How do I make the 'Upload another image' button create another upload image field on the form and save it to the member_images table?

Comments

  • edited 9:40PM
    The file upload types don't save to the database, they merely just upload the file. If you want to upload and save to a field in the database, you will need to create a field to capture that. By default, any field in the table that has a name ending with "image" or "img" will add that extra field for you.

    This process will be quite a bit easier in the upcoming 1.0 release.
  • edited 9:40PM
    Hi admin,
    I understand that I need to create the field, but how do I make the 'Upload another image' button create another upload image field on the form and save it to the member_images table?

    Is it possible?

    And I have this code in my members model:
    $data = (!empty($this->normalized_save_data['image'])) ? $this->normalized_save_data['image'] : array(); $this->save_related('member_images_model', array('member_id' => $values['id']), array('image' => $data));
    Shouldn't this save the image (e.g '01.jpg') in the member_images table?
  • edited 9:40PM
    If you add 'class' =>'multifile' to your file field, then it will give you the ability to upload multiple files. However, as for the processing of those file names to the database, you would need to use an on_after_post hook most likely to do further processing to save them into the database. You member_images model should have a "upload_data" property you can access in that hook which will contain uploaded file information.
Sign In or Register to comment.