I’m building a related extension and module that both need the same path settings. I currently store the variables using the extension’s control panel. Obviously setting it manually on both module and extension is unacceptable. How do I access extension configuration from module code?
For many of our add-ons we use the extension settings for the add-on, even if we don’t need to use a hook in EE’s system. One example of that is our Deployment Hooks add-on. We have a method in our model that selects the settings from the database. Then we set those settings in our MCP constructor (for example). Here are a few links to our code to show what I’m referencing.
https://github.com/focuslabllc/deployment_hooks.ee2_addon/blob/master/deployment_hooks/models/deployment_hooks_model.php#L63 https://github.com/focuslabllc/deployment_hooks.ee2_addon/blob/master/deployment_hooks/mcp.deployment_hooks.php#L67
This approach has worked well for us. Is that helpful?
Erik
That looks good. From a performance perspective I’d like to know a bit more about when exactly that database call will occur, which will be dictated by when construction of the MCP class occurs and whether that load->model method actually constructs a new instance of the model or if some singleton magic is at play. Where can I go to learn more about the lifecycle of EE / CI classes?
This is actually the first time I’ve considered using a model class, I didn’t realise that modules themselves could be MVC. I guess the existence of folders named ‘controllers’, ‘views’, ‘models’ should have been my first clue 😊.
From a performance perspective I’d like to know a bit more about when exactly that database call will occur, which will be dictated by when construction of the MCP class occurs and whether that load->model method actually constructs a new instance of the model or if some singleton magic is at play.
In my case the model is loaded with each MCP requesto my add-on. So any request to “system/index.php?D=cp&C=addons_modules&M=show_module_cp&module=YOUR_MODULE” is applicable here.
Where can I go to learn more about the lifecycle of EE / CI classes?
The lifecycle? Probably just diving into the code. You can checkout the Loader Class docs for CI or dive into the library itself.
Ok reading through the code leads me to the conclusion that every time the load->model() method is called a new instance of the model class is created. I was hoping that there would be some crazy Singleton magic going on a la Spring in Java so you’d only actually create the model once. Although then if the settings changed you’d have to have some way of re-instantiating the model, I guess.
I assume that if I load the model from a module constructor that’s only going to happen once per request, i.e. if one request requires processing a template with multiple tags from the same module that there is only one module instance for all of them. Does that sound right?
Thanks for all your help so far 😊
I assume that if I load the model from a module constructor that’s only going to happen once per request, i.e. if one request requires processing a template with multiple tags from the same module that there is only one module instance for all of them. Does that sound right?
Yes, that sounds correct. I’d also like to note that if you’re using tags in your template you’re now talking about your MOD file and not the MCP file.
mod.your_addon.php will be your template-tag(s) methods mcp.your_addon.php will be your control panel methods
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.