![]() |
Model within a model - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Model within a model (/showthread.php?tid=79534) Pages:
1
2
|
Model within a model - LuxesR - 06-29-2021 Is it possible to include a model in the '__construct' and do the same with the other model? PHP Code: class Users_model extends \App\Models\BaseModel { PHP Code: class Settings_model extends \App\Models\BaseModel { When I do this, I always get a 503 Service Temporarily Unavailable error. Is there a workaround for this? RE: Model within a model - BilltheCat - 06-29-2021 Untested, but this should work: PHP Code: <?php RE: Model within a model - LuxesR - 06-29-2021 Thanks for your response. But can I call the LoggingModel in the UserModel the same way? RE: Model within a model - BilltheCat - 06-29-2021 I can't think of any reason why not, have you tried? To match your model names: PHP Code: <?php PHP Code: <?php RE: Model within a model - paulbalandan - 06-30-2021 If each model is a dependency of the other and called in each constructor, there exists a cyclic dependency, or circular reference. You cannot do that. It will result to infinite recursion of Users model calling Settings model calling Users model calling Settings model and so forth. From your current situation, your models are tightly coupled to each other. You should rethink ways to decouple your code. RE: Model within a model - InsiteFX - 06-30-2021 HINT: Like using a BaseModel and then extending all your models from the BaseModel. RE: Model within a model - LuxesR - 06-30-2021 Thanks for your responses. What InsiteFX suggested was my first idea but I had the same problem with that. I'm using it like this (just like the Settings_model): PHP Code: namespace App\Models; And my BaseModel looks like this: PHP Code: namespace App\Models; So both have the Settings_model and the Users_model because they get it from the BaseModel. I thing this is getting the same error because it is the same idea of what paulbalandan is saying. Or maybe it is because I'm trying to fix my CI3 project in CI4, so it might has something to do that those models aren't correctly converted yet. Are the examples above best practice or do I mis something? RE: Model within a model - paulbalandan - 06-30-2021 By any chance, why do you need an instance of Users model inside Settings model, and vice versa? RE: Model within a model - paulkd - 07-01-2021 Hi, In general, I call models from controllers. However, here is an example where I've called Customers model from Orders model. Hope this helps. Models/BaseModel.php PHP Code: <?php Models/Customers.php PHP Code: <?php Models/Orders.php PHP Code: <?php RE: Model within a model - LuxesR - 07-01-2021 (06-30-2021, 07:07 PM)paulbalandan Wrote: By any chance, why do you need an instance of Users model inside Settings model, and vice versa? Because in some functions of the Settings_model I need data from the users and in the Users_model I need data from the settings. That might not be best practice, but that's the situation. |