![]() |
Custom model methods - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Custom model methods (/showthread.php?tid=79551) |
Custom model methods - Ryubal - 07-01-2021 Hey guys. I'm working on a fairly new project, which is storing all setings in a MySQL table (myproject_settings). There are many cases where I need to fetch a single setting, using something like the following: PHP Code: $this->settings = new \App\Models\SettingsModel(); As you can see, having dozens of those lines throughout my project looks way too verbose. So I was thinking about adding a method to my model directly: PHP Code: class SettingsModel extends Model { And then, in my controller, I could just do: PHP Code: $mysetting = $this->settings->getSetting('mysetting'); My question is.. Is it bad practice to add custom methods directly in the model? I couldn't find anything related on the documentation. Thank you. RE: Custom model methods - ikesela - 07-01-2021 of course your can, its better RE: Custom model methods - includebeer - 07-02-2021 (07-01-2021, 03:39 PM)Ryubal Wrote: My question is.. Is it bad practice to add custom methods directly in the model? I couldn't find anything related on the documentation. On the contrary, writing reusable method is best practice. Having sql query all over the place is bad practice. RE: Custom model methods - Ryubal - 07-02-2021 Thank you, guys. Today I was reading the docs again, and I found in the provided example that it actually encourages using methods inside the model (https://codeigniter4.github.io/userguide/tutorial/news_section.html) Sorry for the ignorance, the doc is a bit messy ![]() RE: Custom model methods - dewaz - 11-09-2021 thank you, i think i can start do the same RE: Custom model methods - aosomidongphucbonmua - 11-10-2021 that nice |