drzrdc1766788 2010-09-29 17:58
浏览 39

这是在MVC应用程序上构建模型的好方法吗?

This is more a "Am i doing it right?" question.

I am using a MVC framework ( Zend Framework ) and was wondering if this is the best way to structure a model.

In this case i have an user groups model and, after coding for sometime, i realized that on my controllers i was repeating some stuff like a query to return visible groups.

$model->fetchAll( array(
     'enabled = 1',
     'visible = 1'
) );

In order to reduce code repetition, i created a method on this model that returns the query for visible groups.

Based on this same situation, i also created many other methods like hidden groups, disabled groups etc.

Is this the best way to avoid code duplication? My concern is that if i kept inserting that code on controllers i would have a big headache if the business rule for visible group changes in the future.

At the same time i am also concerned that, if i follow this new approach, i will end up with a bloated model full of methods to return queries.

How do you deal with this kind of situation?

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • douluanji8752 2010-09-29 18:10
    关注

    if u are writing queries to fetch data in controllers where is Model layer of mvc then?. i would write a seperate layer that would get, add, edit etc. my domain models. u can have a look at repository pattern in php here.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本