Questions on creating a module with 2 db tables

What i'm now trying to do is to create a module that displays recent projects. For each recent project there can be multiple images associated with the project.

My db structure is like this:
recent_project table project_id details published slug recent_project_images table id project_id (which is a fk to recent_project.project_id) image

My question is how do I go about creating this in my model so that a user can add multiple images per recent project.

My current model code is:

class RecentProjects_model extends Base_module_model { function __construct() { parent::__construct('recent_project'); } // method used to list the data in the admin panel function list_items($limit = NULL, $offset = NULL, $col = 'project_id', $order = 'asc') { // the data to list $this->db->select('recent_project.project_id,, recent_project.details, recent_project_images.image, recent_project.slug, recent_project.published', FALSE); $this->db->join('recent_project_images', 'recent_project_images.project_id = recent_project.project_id'); $data = parent::list_items($limit, $offset, $col, $order); return $data; } // used to return form information to the form builder class to render the create/edit screens in the admin panel function form_fields($values = array()) { $fields = parent::form_fields($values); // to limit the image folder to just the projects folder for selection $fields['image']['class'] = 'asset_select images/recentProjectImages'; $upload_path = assets_server_path('recentProjectImages', 'images'); $fields['image_upload'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE, 'required' => TRUE); // fix the preview by adding galleryImages in front of the image path since we are saving it in a subfolder if (!empty($values['image'])) { $fields['image_upload']['before_html'] = '<div class="img_display"><img src="'.img_path('recentProjectImages/thumbs/'.$values['image']).'_thumb'.'" style="float: right;"/></div>'; } //$fields['published']['order'] = 1000; return $fields; }

Do I need to somehow change this line parent::__construct('recent_project'); to look at both tables?

With the code that I have now, if I click on the Recent Projects module in the admin panel, I can see the list of recent projects that I manually added to the database, but if I try to create one, I get this error message:

Error Number: 1054 Unknown column 'id' in 'field list' SELECT id, details FROM (recent_project) ORDER BY `details` asc Filename: core/MY_Model.php Line Number: 646


  • edited 1:46PM
    First, as far as the error, the recent_project table is missing an ID field which is why you are seeing that error.

    With regards to the multiple images, try adding the class "multifile" to the $fields['image_upload'] field which should allow you to upload more then one file at a time. You may want to upload the files into there own specific project folder to help with organization. You can accomplish that by adding a "folder" field to your model and then in your 'upload_path' value you can use the {folder} placeholder which will substitute in the value:
    $upload_path = assets_server_path('recentProjectImages/{folder}/', 'images');
    Then, I would create an additional module for the recent_project_images table (you can set it as "hidden" in it's configuration so it won't appear in the navigation). You can then manipulate the the form_fields() method to display the selected images by scanning the recentProjectImages/{folder}/ folder to pull the image names and then display them by using the 'before_html' attribute. You can display it as a linked image that when clicked will take you to the recent_project_images module to edit.
Sign In or Register to comment.