7

Is it appropriate for a Block class to retrieve data through a function in a ResourceModel class, rather than a Model class?
Or should the Block call the Model which then passes the call to the ResourceModel?

Going direct from Block to ResourceModel is simplest and avoids duplication of possible quite a few functions, but would you consider it to be bad form, breaking best practice patterns?

3

This is perfectly acceptable. In fact, other than instantiating Resource Models in controllers, this is how it's intended to work. A Resource model is a special type of Model in Magento - one that populates its data from a data source.

If you examine Magento core you will see many instances of using Resource Models in Blocks. Many times you don't realize that's what's happening because often this is written like:

Mage::getModel('catalog/product')->getCollection();

A block's method will provide it to the template to utilize - usually to iterate.

1
  • 1
    Thanks for your replay. I think I get you, but what about Mage::getResourceModel('log/visitor')->getStartOfLog() in a Block? Would that not bypass the (domain) Model layer? – fris Jul 5 '15 at 3:58
3

Theoretically, I think it's bad form because someone could have changed the resource model class (very unlikely). But it depends. One practical example is the category and product resource models (and resource collections). If you instantiate them directly, you might not get the appropriate model (flat or normal). I think that's the only instance of where it would actually matter though.

1
  • Agree that it is probably more a case of the theoretically purists vs the every day pragmatists. – fris Jul 5 '15 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.