save related data to another table?

edited December 2011 in Modules
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

  • edited 6:10PM
    Im away from a comp and I've not used the related methods, I'd usually use the on_after_save hook to load the other tables model and do the work there
  • edited 6:10PM
    I have also done on_after_save hook but. don't know how to save more then one data on other table.
  • edited 6:10PM
    In the form itself, you can set all your profile attributes to have field names that create an array when posted. For example:
    // 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; }
  • edited 6:10PM
    Thanks, it worked.

    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.
  • edited 6:10PM
    The newly inserted id should be in the $values array. Try adding $p->profile_id = $values['id'] in the foreach loop.
  • edited December 2011
    I want to first add users on on_before_save hook of profiles_model and then take the inserted id and insert or update to profile table using foreign key user_id.
    $posted = $_POST
    if (!empty($posted['profile']) AND !empty($values['id']))
    {
    $this->load->model('users_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
    if(!empty($posted['user_id']) AND ($posted['user_id'] != 0)){
    $this->users_model->delete(array('id' => $values['user_id']));
    }
    foreach($posted['profile'] as $user)
    {
    $p = $this->users_model->create();
    $p->name = $profile['name'];
    $p->email = $profile['email'];
    $p->save();
    }
    //-----------here how to get the inserted id of this users_model-----------
    }
    return $values;
    //--------after this value is returned i will add the returned id of users to profile as user_id------
  • edited 6:10PM
    $p->save() will return the id value if saved without errors:
    $user_id = $p->save();
    Also, after save, you can access the user id like so:
    $user_id = $p->id;
Sign In or Register to comment.