Importing module with same db col name

edited August 2015 in Modules
I am trying to create a module form which includes the form fields of a hidden module.
The problem is that both the tables of the module has columns with same name.

So, when i do following in the public function form_fields($values = array(), $related = array())

$CI =& get_instance();

$CI->load->module_model('istu','stu_images_model');
$fields2 = $CI->stu_images_model->form_fields();

$fields = array_merge($fields, $fields2);


The fields overlap & the form is not shown correctly.

Is there any way to include both the form elements even when they have same column name in db?

Comments

  • edited 10:13AM
    You will need to change the "key" value of the $fields array of one of them to prevent it from being overwritten. Perhaps something like this:
    $fields2 = $CI->stu_images_model->form_fields(); $fields2['same_name'] = $fields2['name']; unset($fields2['name']); $fields = array_merge($fields, $fields2);
  • edited 10:13AM
    Thanks for the help.

    I would also add to the discussion about how to save the data for other table using on_after_save() function
    public function on_after_save($values) { parent::on_after_save($values); $posted = $this->normalized_save_data; $CI =& get_instance(); $CI->load->module_model('stu','stu_images_model'); if (!empty($posted['id'])) { $posted['id'] = $posted['ci_id']; //ci id is the id for the stu_images_model $post = $CI->stu_images_model->save($posted,TRUE,FALSE); $posted['id'] = $posted['stu_id']; //stu id saves the id for the students_model } return $values; }
    Is this the right way ?
  • edited September 2015
    Using the model hook for saving is a good idea. There is also a "save_related" method that can be called in the on_after_save hook that has the following signature and is found on MY_Model (the array key/value parameters are admittedly a little strange):
    // -------------------------------------------------------------------- /** * Save related data to a many to many table. To be used in on_after_save hook * $this->examples_model->save_related('examples_to_categories', array('example_id' => $obj->id), array('categories_id' => $_POST['categories'])); * * @access public * @param mixed the model to save to * @param array key is the column name, and value is the value to save * @param array key is the column name, and the array of data to iterate over and save * @return boolean */ public function save_related($model, $key_field, $data) { $this->_check_readonly(); $CI =& get_instance(); $model = $this->load_model($model); $id = current($key_field); $key_field = key($key_field); $other_field = key($data); $data = current($data); // first remove all the articles $CI->$model->delete(array($key_field => $id)); // then read them $return = TRUE; foreach($data as $val) { $d = $CI->$model->create(); $d->$key_field = $id; $d->$other_field = $val; if ($d->save()) { $return = FALSE; } } return $return; }
  • edited September 2015
    @admin
    How will it work if its a One-to-One relationship between two tables??
    What i did in http://forum.getfuelcms.com/discussion/2304/importing-module-with-same-db-col-name#Comment_8565
    works if i am updating the primary table. It updates the value in Secondary/foreign table which is mapped one-to-one.

    But when i am creating a new entry in the primary table --- corresponding row is not created in the foreign/secondary table.
    { instead i got an entry with id=0 }

    Also, what is the hook when one deletes an entry in primary table which deletes the one-to-one mapped entry in foreign table.
    Also, if one tries to delete an entry in foreign table, it prohibits since its mapped to the primary table? Are there any hooks for that?
  • edited September 2015
    The module 'create' function is working fine in other modules with no dependency like the one said above.
    However, For those modules which includes form fields from other modules...(1-to-1 dependency)
    whenever i am trying to create a new entry -- it is saved in the db. But with an id =0.
  • edited 10:13AM
    It sounds like you need to save to the primary table and get the idea (which can be found in the $values['id'] in the on_after_save model hook). Use that for the id instead of the $posted because it automatically gets updated to the newly saved ID before it gets sent to the on_after_save hook.

    For deleting, there are 2 hooks, on_before_delete and on_after_delete:
    http://docs.getfuelcms.com/general/models#hooks
Sign In or Register to comment.