When i click on create in advanced modules how can i get different column of different table

edited April 2012 in Modules
My models is like this, when i click on create i get the tables of fuel_tableentry but i want to get the columns of other table how to get that?

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once(FUEL_PATH.'models/base_module_model.php');
require_once(MODULES_PATH.'/partywise_register/config/partywise_register_constants.php');

class example1 extends Base_module_model {

public $required = array('Name','number', 'Date', 'number1', 'InvoiceNo', 'InvoiceDate', 'MatId', 'Width', 'Thickness', 'Quantity');
protected $key_field = 'number';
function __construct()
{
parent::__construct('fuel_tableentry');// table name
}

function list_items($limit = NULL, $offset = NULL, $col = 'fuel_tableentry.Date', $order = 'asc')
{

$this->db->select('fuel_tableentry.number, fuel_tableentry.Date,fuel_matdescription.Description ,fuel_tableentry.fThickness, fuel_tableentry.fWidth ,fuel_tableentry.Status');
$this->db->join('fuel_matdescription', 'fuel_matdescription.MatId = fuel_tableentry.MatId', 'left');

$data = parent::list_items($limit, $offset, $col, $order);
return $data;
}
}

Comments

  • edited 8:23AM
    You'll need a form_fields() method in your model to alter the 'create' fields..

    In there you could load the model for your other table and run it's form_fields() method and merge the two, something like:

    public function form_fields($values = array()) { # Fields for 'fuel_tableentry' $fields_1 = parent::form_fields(); # Fields from other table $this->load->model('other_table_model'); $fields_2 = $this->other_table_model->form_fields(); $fields = array_merge($fields_1, $fields_2) return $fields; }

    Validation will likely strip all the fields from other_table_model on save though so use the $this->normalized_save_data array to get at them.
  • edited 8:23AM
    Thanks Lance i'll try your method
    Appreciated for your time
  • edited 8:23AM
    Thanks Lance your method works
    but while loading i tried with
    $this->load->module_model(EXAMPLE_FOLDER, 'example'); and this worked instead of
    $this->load->model('other_table_model');
  • edited 8:23AM
    I still have one more question i am not able to load the columns?
    i have one field where name gets loaded from database it is not populating
    properly
  • edited February 2016
    This may have to do with how the values are being set on the form fields. By default, FUEL uses the find_one_array method from your model that returns the values but just for that model. In your case, you are combining 2 different models. I can think of 4 ways you can grab values from the second model to help populate your form fields:

    1. The first, and what I would probably recommend, would be to select and join the other tables values similar to what you did in the list_items method but do it in the model's _common_query() method:
    function _common_query() $this->db->select('fuel_tableentry.*, fuel_matdescription.Description.....'); $this->db->join('fuel_matdescription', 'fuel_matdescription.MatId = fuel_tableentry.MatId', 'left'); }

    2. The second is to overwrite the find_all_array method on your model to retrieve data from both models.
    3. The third is to create a different method on your model and then have your module config use that instead of find_one_array by changing the "edit_method" parameter.
    http://www.getfuelcms.com/user_guide/modules/simple
    4. The fourth way is to set the values in your form_fields method for the second model by grabbing the values perhaps using something like:
    public function form_fields($values = array()) { # Fields for 'fuel_tableentry' $fields_1 = parent::form_fields(); # Fields from other table $this->load->model('other_table_model'); $fields_2 = $this->other_table_model->form_fields(); if (!empty($values['id'])){ $fields_2_values = $this->other_table_model->find_by_key($values['id']); foreach($fields_2_values as $key => $val){ if (isset($fields_2[$key])){ $fields_2[$key]['value'] = $val; } } $fields = array_merge($fields_1, $fields_2) return $fields; }
    Now note, this is just retrieving values. For saving the values, you may need to use the on_after_save hook and perhaps within that hook the save_related method:
    http://www.getfuelcms.com/user_guide/libraries/my_model
    http://www.getfuelcms.com/user_guide/modules/tutorial (talks about that in the "Adding an After Save Hook" section)
Sign In or Register to comment.