![]() |
hi,
i need to know if i have to load a wanted model in every helper function again and again or is there a way to load it once and how i can access them. I try in my helper " Code: use CodeIgniter\CodeIgniter; Info: the departmentmodel extend the model Access with $this failed = Using $this when not in object context What is the best way to load the model and make it accessable in the helper functions?
12-27-2024, 06:13 AM
(This post was last modified: 12-27-2024, 06:26 AM by captain-sensible. Edit Reason: addition )
a start of one of my models is :
Code: <?php to use the model in a controller : Code: $this->handle = new BlogModel(); no helpers , no messing , field are declared in properties model if you follow the appropriate architecture and place your models in app/Models and use namespace, Code: namespace App\Models; Then when you instantiate with : Code: $this->handle = new BlogModel(); then class will be looked for, found and be instantiated. because we are extending "model" then various things are inbuilt
@captain-sensible
Thank for your detailed code, it makes something clear for me. But let me ask again: Is there any way to setup a global db-connection, to avoid, setup the db connection in each model again and again? Also my main-question, is how i can load the model in the helper, look at my code, i dont understand why i cannot go in this way...
@usol global connection ? To use a model class ,you have to instantiate it. When you instantiate it connects to default database; but its only one line to switch to another database set out in app/config/Database.php so why write more lines of code that I dont have to ?
|
Welcome Guest, Not a member yet? Register Sign In |