Using ORM classes directly from the controller in MVC, bad practice?

前端 未结 4 2003
抹茶落季
抹茶落季 2021-01-12 13:06

I\'ve recently delved into using an ORM in my CodeIgniter application and one i\'ve gone for is Propel. Now this gives me the power to basically use Propels classes as the \

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 13:48

    It depends a lot on what you are doing and why. in this example you are putting a limit clause in the query - is that business or display logic? From my perspective, it's hard to argue that it's business logic - that I get back 10 elements is irrelevant to the model - that's just how many I think makes sense to using in one page. If you want that rule to be consistent across controllers, you could set some config value to enforce consistency. But putting it in the model just makes the model needless large (there's a difference between fat models and obese models)

    I would say that limits, orders and offsets are often not business logic. Even a simple where might or might not be depending on the case. If there's a join there, it's a sign that something is wrong.

    The example from Jan Fabry is mostly pretty good. filterByTitle looks about the same to me as titleContainsWord. filterByPublishedAt(array('max' => time())) is much worse than ->published(). In general, the less you controllers need to know about the inner data structure, the better.

提交回复
热议问题