Difference between Model and Model Resource


Im starting a project with Magento. Let me first say, that i come from the MS world, i have never used PHP before, and while im catching up quite rapidly i'm def a php newb.

I can't quite seem to grasp the differences between the model, resource model, resource and entity objects and how they are related in the magento architecture.

I've tried googling for days now and i have found little info, and what i have found doesn't really explain it!

7/1/2011 10:52:43 PM

Accepted Answer

Magento is far from typical of PHP development and what it calls things don't always agree with what other people call them. Magento splits the model layer up into two parts as you've recognised, with the Model handling the business-logic, and the Resource part only dealing with talking to the database - the theory being that if you decided on a data-store that wasn't addressable with PDO, you'd be able to swap in a different resource to handle/loading and saving, without any of the logic that surrounds those things.

The two different types of Resource that magento does have are Entity and Mysql4 - the latter being a pretty standard table-gateway system, where the Resource maps to a table in the database, the former is a bit more complicated. In order to make it really easy for users and developers to add custom fields to things, the main parts of magento (products, customers, orders etc.) are all represented by what's known as Entity-Attribute-Value system (EAV) where rather than having a typed column per value, you have a row with a key (c.f. column name) and a value. The Entity Resources handle the mapping of these eav tables to the field-array that holds the data.

To emphasise though, this is far from typical of a PHP application and if your intention is to learn PHP, I'd pick a different system.

1/22/2010 7:44:44 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow