save related data to another table?
I want to save some added fields data to another table of another module. how is it possible.
save_related will save two foreign keys of two tables in seperate table, if I have learned correctly.
table profile
++++++++++++++++
| id | name | email |
table users
+++++++++++++++++++++++++++++
| id | username | password | profile_id |
On form of profile page I should be able to add username, password and profile_id = id
how can I do this.
Comments
// in users form_fields method... function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); $fields['profile[name]'] = array('label' => 'Name'); $fields['profile[email]'] = array('label' => 'Email'); return $fields; }
Then in your on_after_save hook, you could do something like the following:
function on_after_save($values) { $posted = $_POST if (!empty($posted['profile']) AND !empty($values['profile_id'])) { $this->load->model('profiles_model'); // first delete any existing profile information... may not be necessary if the email is set to a unique index in which case the value will just be updated $this->profiles_model->delete(array('profile_id' => $values['profile_id'])); foreach($posted['profile'] as $profile) { $p = $this->profiles_model->create(); $p->name = $profile['name']; $p->email = $profile['email']; $p->save(); } } return $values; }
need a small help what i want to do is:
retrieve data from profile form and insert part of it to users form for login_name and password first and
take the insert id of users table and put it into profile table as user_id.
$user_id = $p->save();
Also, after save, you can access the user id like so:
$user_id = $p->id;