2008年7月21日星期一

Re: [fw-mvc] How to populate a select form element from a different module/controller/model?

Creating a view helper that uses the Language model to pull the list of
languages is a perfectly valid solution to this problem. I would assume
you could then tell Zend_Form to use that view helper to display the
drop down.

Also, there's no technical reason why models can't access other models.
Whether you do so or not is entirely up to you and how you want your
application's architecture to work. It's probably not a good idea to
force models to depend on controllers to access other models, since the
whole model layer is better off being completely independent of the
controllers (or any other code that uses it). Think about what you'd
have to do if you had to build a CLI version of the application: you'd
probably need different controllers, which would break many models, but
you still need the same business logic.

Regards,
Bryce Lohr

Codiac wrote:
> Hi All,
>
> I know some of this (MVC) stuff has been discussed before, but I'm still
> curious and looking for guidance, advice, clarification or perhaps a best
> practice for the following:
>
> a. I have a module (controller, model, views) for creating and managing
> static pages and I also have a separate controller and model for managing
> languages.
> b. Using a form (Zend_Form -> select element, dropdown list), a user can can
> set the language for each static page. The form itself is instantiated
> inside the PageController.
>
> My specific problem:
> How and where (controller/model/view helper) would I fetch the language
> (array) data, located in the Language model with access to the languages
> database table, for the select element/dropdown list in the Page form
> (located in the PageController)?
>
> Solutions I can think of:
> 1. Create a query in the Page Model, that fetches the languages from the
> other table in the database. Easiest, but what if the language table
> changes? Then I need to update the query in both the Page model and Language
> model (not very DRY), instead of only the Language model.
>
> 2. Require or include the Language model in the Page Model, retrieve the
> language data and return the data to the PageController that sets it the
> Page form. Wow!
>
> 3. Require or include the Language model in the PageController, retrieve the
> form element in the page form and using the language model set the options
> on the select element.
>
> 4. Anymore solutions exist?
>
> My general problem:
> Can models access other models directly, or should there always be a
> controller between them? Which creates a different problem when Controllers
> need to connect to other Controllers to get to the model data.
>
> I'm actually quite confused to see that a View (helper) can have read access
> to a model, controllers setup and access models, but also the possibility
> that models can access other models as well.
>
> Regards, TJ.
>

没有评论: