Importing module with same db col name
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
$fields2 = $CI->stu_images_model->form_fields(); $fields2['same_name'] = $fields2['name']; unset($fields2['name']); $fields = array_merge($fields, $fields2);
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 ?
// -------------------------------------------------------------------- /** * 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; }
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?
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.
For deleting, there are 2 hooks, on_before_delete and on_after_delete:
http://docs.getfuelcms.com/general/models#hooks