![]() |
Load model in helper - 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: Load model in helper (/showthread.php?tid=92233) |
Load model in helper - 4usol - 12-27-2024 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? RE: Load model in helper - captain-sensible - 12-27-2024 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 RE: Load model in helper - 4usol - 12-27-2024 @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... RE: Load model in helper - captain-sensible - 12-28-2024 @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 ? |