Welcome Guest, Not a member yet? Register   Sign In
1 controller = 1 model VS. 1 controller = multiple model
#15

(08-27-2018, 11:12 PM)kaitenz Wrote: What if I load a model to a specific controller but that model is dependent to the other model?
Should I load also the "other model"?

You have quite few options here, and depending on your coding principles, one might sound better than other.

1. Autoload all your models, all the time. I can't help but think this could be massive waste of resources having to load in additional PHP script files, but for some small apps, this could be acceptable solution.

2. You could load other model in parent models __construct() method. This way whenever parent model is created, it's certain that necessary other models are available too. I'd imagine there might be some cases where trying to load model every time new instance of parent model is created could also affect performance.

3. You load child model code only in methods that require use of that model.

4. You load child models on controller level. Probably the most likely way to break your code if you accidentally forget to load model for specific action, and forget to test it.

5. Use dependency injection. This is probably the hardest concept to understand, but the cleanest / transparent way to code, plus as by-product, the code should be easier to unit test. It does involve loading models in controllers, but instead of letting models handle all the stuff behind the scenes, you load data on controller level, then pass instances between models, instead of IDs.
Reply


Messages In This Thread
RE: 1 controller = 1 model VS. 1 controller = multiple model - by Pertti - 08-28-2018, 12:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB