database, model loading in MVC |
Hi,
I want to know how often need to load / connect database / model and session in these 3 pieces of code (controller, model, view). I am facing strange even the constructor is not working :-) In following code, I need to connect database in every function before using a select from database, the same case with session, and model my question is "Do I really need to connect and load (database, Model, session) in every file, ie Model, view? for example I have main_controller, MainModel, and main_view. main_controller: Code: <?php } MainModel: Code: use CodeIgniter\Model; View: Code: <!DOCTYPE html>
01-28-2023, 12:03 AM
(This post was last modified: 01-28-2023, 12:04 AM by InsiteFX. Edit Reason: spelling error )
If your using a single database connection and table it is load for you when you load the your model.
You only need to load the database connection if you need to manually connect to different tables etc; This is how to load the sessions in the BaseController. PHP Code: <?php What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks for reply.
1. What about another database? 2. How to call a function in view? do I need to load the model in every view? use App\Models\MainModel; $MainModel = new MainModel; 2. what about session? I tried to add session / another database to BaseController but no luck.
If you really need a model in a view, load it in a controller and pass it to a view.
https://www.codeigniter.com/user_guide/o...o-the-view
(01-28-2023, 01:21 AM)kenjis Wrote: If you really need a model in a view, load it in a controller and pass it to a view. In model where can I define the another / second database connection, so can be used by multiple functions in model, I tried Construct but no luck. I am using Codeigniter 4. (01-28-2023, 08:19 AM)nadeem14375 Wrote:(01-28-2023, 01:21 AM)kenjis Wrote: If you really need a model in a view, load it in a controller and pass it to a view. > See: https://codeigniter.com/user_guide/datab...parameters > See: https://codeigniter.com/user_guide/model...e-database > See: https://codeigniter.com/user_guide/datab...-databases
(01-28-2023, 08:25 AM)superior Wrote:Hi Superior,(01-28-2023, 08:19 AM)nadeem14375 Wrote:(01-28-2023, 01:21 AM)kenjis Wrote: If you really need a model in a view, load it in a controller and pass it to a view. Thanks for your reply. I am talking how / where to make the connection, I can use the variable in any Method of model. for example: the following code make the connection. Code: // anotherDb ------------------- Code: public function get_school_list($school_name) { |
Welcome Guest, Not a member yet? Register Sign In |