CodeIgniter Forums
Is conceptually correct to access DB from Controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Is conceptually correct to access DB from Controller? (/showthread.php?tid=76249)



Is conceptually correct to access DB from Controller? - YanKleber - 04-25-2020

This is more a conceptual question. I am learning MVC and I recently watched a video that supposed to show how to implement a simple CRUD by using the basic query builder methods. Everything was doing well when he called the method save() directly from the Controller.

Maybe I just didn't get correctly the philosophy behind the MVC but it looks totally odd to me.

What am I missing here?

Dodgy


RE: Is conceptually correct to access DB from Controller? - jreklund - 04-25-2020

If it's the Models save() function it's indeed correct. You trigger Models from Controllers, but should not use the query builder itself in Controllers.

You can read more about it in the userguide: https://codeigniter.com/user_guide/concepts/mvc.html


RE: Is conceptually correct to access DB from Controller? - YanKleber - 04-25-2020

Nope, he was using the query builder save method.
As I imagined the guy was doing it wrongly.
Thanks for the answer!


RE: Is conceptually correct to access DB from Controller? - jreklund - 04-25-2020

There are nothing in the code that's stopping you of doing so. If you need to access one table, only ones, in an controller. It may be just worth it. But you are technically breaking MVC, but I have done it for small things.


RE: Is conceptually correct to access DB from Controller? - YanKleber - 04-25-2020

(04-25-2020, 01:21 PM)jreklund Wrote: But you are technically breaking MVC

Cool, thanks, I just wanted to be sure that I wasn't fantasying!

Big Grin