Related Model

edited August 2011 in Modules
Hey Team,

me again. :). Hope it's not too hot, wherever you are.

Like the title says, i do have problems with my idea of "related models."

scenario:

available models
- user_model - user_profile_model

within the user_model i´ll create with the "create" method a new user. in the on_after_insert hook, i
want to create a entry with user_profile_model:

so, in the user_model i add:

function on_after_insert($values) { $this->load->module_model(FUEL_FOLDER, 'user_profile_model'); $value = array('fuel_user_id' => $this->db->last_insert_id()); $this->user_profile_model->insert($value); }

So. Now my problems:

1. The user_model wont redirect back to the "user/edit/ID" page, after successful adding of both entries. it saves the data, but without a success message - the "create" page stills shows up.

2. In some cases i have the feeling, some db values are still in cache, is that possible?

Example: if i try with the "user_profile_model->insert($value)" method, the $value array does have just one value, but in some cases, the array contains values from the previous query, it ends with an db error -> am i going crazy?

Thank you very much for your time!

Comments

  • edited 11:08PM
    ok.

    maybe both questions are answered by myself! :)

    i start in the user_model -> on_after_insert an try to add a record with the user_profiles_model ($this->user_profile_model->insert($value));

    Thats not quite possible because the user_profile_model again calls the own on_save_after etc., is that right?

    My solution: in user_model -> on_after_save() :

    $values = array('fuel_user_id' => $values['id']); //only insert if new $record_exists = $this->users_profiles_model->record_exists($values); if(!$record_exists) $CI->users_profiles_model->insert($values);

    What do you think, is that a clean solution?
  • edited August 2011
    Your first method should work. However it is $this->db->insert_id() and not $this->db->last_insert_id() and the model name is $this->users_profiles_model (with an "s")
Sign In or Register to comment.