saving to diff table

edited August 2014 in Modules
I created a form which fields from a table A, but the rest of fields are manually created which data will be saved to another table B. i am coding the model operation at model layer of table A. I have two scenerio now: insert and update

i think for update, i can use on_after_post($values). Here i think the $values holds both the table A and table B values which i think i can save to table B first then return the the values with keys matches the columns in table A. ( i think this approach should not have problem)

but for insert, i cant use on_before_save, this is because table B have a foreign key refer to table A. Only a record created on table A, i.e. id, then i can use this id to save in table B. So i think i can only use on_after_save($values), then my questions is as $values holds key and values from both table, will saving to table A by system cause problem due to difference in column name? if i use on_after_save($values) how can i get the just created record id of table A for me to save data on table B?

Comments

  • edited 8:24AM
    I'm not quite sure I follow the question, but the $values array automatically injects the "id" into the $values array after it saves and is available after the on_after_save hook. Also, not sure if you know this but the save method will return the insert ID.
  • edited August 2014
    sorry, why on_before_update($values), the $values only contains those in Table A, not table B? how can i retrieve posted data for Table B in on_before_update
  • edited 8:24AM
    You can load the model and retrieve it with the foreign key value:
    $CI =& get_instance(); $CI->load->model('tableB_model'); $tableb_record = $CI->tableB_model->find_by_key($values['tableb_id']); ....
  • edited 8:24AM
    sorry but the thing is i need to parse those data first before saving to table B. ur solution is to find values on table B not getting values from form.

    Maybe i should be specific.
    I have two tables, one product table (id, name) another is price table with product_id refer to product table(id, product_id, size, price).

    so i created form using form builder with fields : product name, size1, price1, size2, price2

    so when i save the data, i need to first save the product name to product table, then save the sizes to price table.

    i used on_after_post($values), but i guess on_after_post are not able to distinguish create or update action which will use different queries. So i thought to use on_after_create($values) and on_before_update($values). But then i discover that the $values only contains form posted data corresponsding to product table, the datas for price table from form are missing..

    what shall i do ?
  • edited 8:24AM
    There is no on_after_create hook but there is an on_after_save(). To get the prices information you can use the model property "normalized_save_data" like so:
    function on_after_save($values) { // should contain all data submitted including price data $data = $this->normalize_save_values; $CI =& get_instance(); $CI->load->model('prices_model'); // delete all old price values $where = array('product_id' => $values['id']; $CI->prices_model->delete($where); // now save the prices value... if prices area in array, you can loop through it $price1 = $CI->prices_model->create(); $price1->price = $data['price1']; $price1->size = $data['size1']; $price1->save(); $price2 = $CI->prices_model->create(); $price2->price = $data['price2']; $price2->size = $data['size2']; $price2->save(); return $values; }
  • edited August 2014
    actually need some more information on this

    on_before_save() & on_after_save() // work on both create and update?
    on_before_update()&on_after_update() //work on update only?
    on_before_post()& on_after_post() // work on both create and update?

    can you arrange in sequence the call of these hook? are they in this order

    on_before_post()
    on_before_save()|on_before_update()
    on_after_save()|on_after_update()
    on_after_post()
  • edited 8:24AM
    That is correct. You can look at the MY_Model::save method to see how the hooks run when you use save. on_before_post and on_after_post hooks are actually called in the fuel/modules/fuel/controllers/module.php controller.
  • edited 8:24AM
    then i have a problem, how can i distinguish create and update action, so we can call different sql queries
  • edited 8:24AM
    There is an "on_before_insert" and an "on_before_update" hook you can use and they get run on the methods "insert" and "update" respectively. The save method calls the "on_before_insert" method if a key value is set (e.g. id) otherwise it runs "on_before_update". Alternatively you can always check to see if an $values['id'] is set to determine if it is an insert or an update:
    function on_before_save($values) { if (!empty($values['id'])) { // run update code } else { // run insert code } }
    http://docs.getfuelcms.com/general/models#hooks
  • edited 8:24AM
    clearer idea on the whole process now, thanks
  • edited 8:24AM
    i did another check on this problem. and i discover that

    if there are three functions in the program

    on_before_insert()
    on_after_insert()
    on_after_update()

    if on_before_insert function runs,
    then on_after_insert will NOT be excuted but straight away jump to on_after_update, i am not sure if this is logical. but there are scenerio i need to set some values before inserting and i need to execute some queries after_insert.......
  • edited 8:24AM
    Do you have an example of the problem you are seeing? Are you following the execution through the MY_Model::save() method?
  • edited 8:24AM
    no actually, i use pdo raw query to insert into the table...i removed on_before_insert, the program now identify both on_after_insert and on_after_update...
Sign In or Register to comment.