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
$CI =& get_instance(); $CI->load->model('tableB_model'); $tableb_record = $CI->tableB_model->find_by_key($values['tableb_id']); ....
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 ?
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; }
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()
function on_before_save($values) { if (!empty($values['id'])) { // run update code } else { // run insert code } }
http://docs.getfuelcms.com/general/models#hooks
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.......