extending module

edited March 2011 in Modules
Hey.

i've created a module with some tables. one of them (entries) has an second table (entries_meta) for additional infos.
in the method form_fields of the entries model i need those fields from the entries_meta. how can i do that?
create a own model for entries_meta? or just create a loop and add those fields to the $fields array and save them with the the on_after_save hook? (if i should add them to the $fields array, can i transform the db-fields automatically into form elements? (with your form_helper)

what's your opinion about that?

Comments

  • edited 11:43PM
    I would probably create another model for the entries_meta. This will allow you to leverage the entries_meta_model form_fields(), save, and delete methods within your entries_model. I would probably do what you are already thinking using the on_after_save and on_after_delete to save and cleanup on delete.
  • edited 11:43PM
    really?

    i need then a short advise:

    how can i include the form_fields from one module into another?

    $fields = array(); $meta_fields = $CI->meta_model->form_fields(); array_push($fields, $meta_fields);

    and to save?

    $CI->meta_model->save();
  • edited 11:43PM
    Yep... you would need to make sure it's loaded first:
    $CI->load->model('meta_model');
  • edited 11:43PM
    :)

    but how do i set the ID?

    entries_table: id => auto_increment title => varchar entries_meta_table: id => auto_increment entryID => int

    how do tell the meta_model the recent entry ID?
  • edited 11:43PM
    form_fields() has a $values parameter that gets passed to it. May want to try something like this:
    function form_fields($values = array()) { $fields = parent::form_fields($values); $CI =& get_instance(); $CI->load->model('meta_model'); $meta_values = array(); if (isset($values['id']) { $meta_values = $CI->meta_model->find_one_array(array('entryID' => $values['id'])); } $meta_fields = $CI->meta_model->form_fields($meta_values); $fields = array_push($fields, $meta_fields); /*..... ANY OTHER LOGIC LIKE FIELD ORDERING, ETC ....*/ return $fields; }
  • edited 11:43PM
    hey mike.
    thank you so much for your time. actually, i am already over that.

    i stuck at the save part.

    let me show you my example:

    my entry_model:

    - id
    - first_name
    - last_name
    - etc.

    my entry_meta_model:

    - id
    - entry_id (the id from entry_model)
    - country
    - zip
    - etc.


    in my entry_model in on_after_save :
    $CI->meta_model->save();

    but i have logical problems with my meta_meta_model. who can i tell that model to update the right entry_id - which came must be the ID from entry_model?

    Things i noticed for my entry_meta_model (correct?):

    $key_field = entry_id (?)
  • edited 11:43PM
    on_after_save() is passed an array of saved $values which will include the entry id field.
Sign In or Register to comment.