Hi,
I think this is a simple question. It will probably draw attention to serious design flaws in my app, but I'll still bravely ask for advice!
My app deals with channels, which can be chained together (that's all you need to know at this stage!). Using my own interpretation of MVC, I have set up a model, view and controller for each of the "modules" in my app - i.e. channels and chains.
The channels model provides business logic for CRUD operations on the channels table (via an external DB mapper); the chains model does the same for the chains table.
Now, I've got to the stage in the chains module where I want to add a channel to a chain. When viewing a chain in the browser, I should be able to click the link which says "add a channel" and be presented with a list of channels to choose from. On clicking the link, the following needs to happen:
- the chain controller will need to ask the chain model to load the current chain (passed in with $_GET['chain_id'] )
- the chain controller needs to build a list of channels
- the chain and the channels are passed to the chain view, which makes a form to select a channel
And here's the problem - how does the chain controller get the list of channels? Should there be a method in the chain model called get_all_channels(), or should I use the existing get_all_channels() method in the channel model to avoid duplication?
I can see two options here: either the chain controller needs to have access to two models (chain model and channel model), or the chain model needs to provide an interface to some of the channel model's methods. I'm not sure how I would do the latter - perhaps make static methods?
Later, other objects will need to be added to the chain like routers and filters. I'm leaning towards the idea of the chain controller having access to lots of models (all stored in a registry so they can be accessed easily). Is this best practice in my case do you think?
Thanks!
MVC - I need two models!
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Well there is no reason why you can't have multiple Models -- almost all MVC systems do -- especially if you consider things like the Request and Config to be datasources as well. It seems like you have two choices: either have multiple Models or have the Chain Model composite the Channel Model internally. I think the latter would probably be better because then the View only needs to deal with the Chain Model and it also gets additional functionality to deal with Channels included.
(#10850)
Aah - nice idea. How would you approach your second idea? Would the chain model create a channel model whenever it was asked to do something that required it? For example:
Something like that?
Code: Select all
class chain_model extends model
{
function _load_channel_model()
{
// standard singleton, or perhaps store it in a registry...whatever
}
function get_installed_channels()
{
$channel_model = $this->_load_channel_model(); // or $channel_model = registry::get('channel_model');
return $channel_model->get_installed_channels();
}
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Exactly! ... the point of the Composite pattern is to combine the interfaces of multiple classes into a single class. You can decide whether you want to get the Channel object within specific methods (like you have shown) or to create one in the Chain constructor to be always available. You'll probably know which is best for your implementation.
(#10850)