MVC - I need two models!
Posted: Sat Jan 06, 2007 2:04 pm
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!
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!