![]() |
Load Dynamic Model - 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 Dynamic Model (/showthread.php?tid=75508) |
Load Dynamic Model - lahoussaine - 02-13-2020 Hello I would like use a dynamic model, i want use loop to load multiple model dynamically, in CI3 work fine and in CI4 doesn't work this is an example $array = array("Roles"); foreach ($array as $key => $value) { $UsersModel = new RolesModel(); // this work fine $module = $key.'Model'; $module = new $module(); // this return error : Class 'RolesModel' not found } can you help me ! RE: Load Dynamic Model - falko - 02-13-2020 Add the "use" statement above your class. I.e. use App\Models\RolesModel; class x extends Controller { } RE: Load Dynamic Model - lahoussaine - 02-13-2020 (02-13-2020, 01:49 PM)falko Wrote: Add the "use" statement above your class. This is top of my Controller : Code: namespace App\Controllers; so when i use this syntax Code: $UsersModel = new RolesModel(); wheen Code: $variable = "NameOfModel"; any idea !! RE: Load Dynamic Model - lahoussaine - 02-13-2020 finally i found a solution for this problem we have to must declare the full namespace path of a class Code: namespace App\Models for help thx RE: Load Dynamic Model - swapnil0804 - 09-18-2022 (02-13-2020, 04:43 PM)lahoussaine Wrote: finally i found a solution for this problem if you are in controller then you need something like this $m="\App\Models".'\\'.$j['model']; $model=new $m(); |