I created a module and I have a collection model class(under Model/Mysql4/MyModule/Collection.php), resource class (Model/Mysql4/MyModule.php), abstract model class (Model/MyModule.php) and a setup class (Model/Resource/Mysql4/Setup.php).

I really created them and my module worked perfectly. But I really don't know exact differences between all those models.

I really don't know which function should I put to which file. In a dummy example, I have a function to get all categories with 3 products inside. Where should I define my function and in which model ?

  • possible duplicate of Why magento use resource and mysql4 model collection? Jul 17, 2014 at 7:34
  • Thanks @FabianBlechschmidt You are somehow correct. I really know the overal differences , but currently I really don't know where to put my function? I confused with all these...What do you suggest ? Thanks for your comment and answer on that question. It was helpful. Jul 17, 2014 at 7:36
  • well, have a file like (Model/Mymodel.php) and make it extend your abstract model. Then, define some functions there. Jul 17, 2014 at 7:37
  • @JulienLachal Thanks for your suggestion, but why there ? I mean why not in collection model ? What will happen if I put it to wrong file ? How can I know my function related to which file here ? Thanks again. Jul 17, 2014 at 7:38

1 Answer 1

  • Resource collections are a list of models, everything which filters the collection, orders it or joins stuff into it belongs here.
  • resource models are the ORM (more or less), so everything which communicates with the database belongs here. getItemById(), createNewItem(), etc.
  • Model is a data container and the business logic so everything else belongs here.

But depending on the complexity of the business logic I would think about creating more models (without any extend) and helper (when the logic doesn't change any inner status)

  • That's whay I wanted to know. You are the man :) Jul 17, 2014 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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