CodeIgniter Forums
save() with multiple tables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: save() with multiple tables (/showthread.php?tid=76389)



save() with multiple tables - yucar - 05-08-2020

Hi! I'm new with CodeIgniter 4 (and with web frameworks in general), and while I was doing a project I've encountered a problem. I want to add an element to a DB, but I'll need to write into two different tables (one of them with a foreign key to another). Can I achieve it with $model->save() or do I have to use another method to achieve this (like a DB query with JOIN or something like that)?

Thanks in advance  Smile


RE: save() with multiple tables - php_rocs - 05-08-2020

@yucar,

The approach that I've always have taken is update the base part of the record first then update the related table second but within the same model. Specific criteria are meet which allowed the secondary table to be updated.