Skip to content

Instantly share code, notes, and snippets.

@Vinai
Last active July 6, 2021 05:25
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Vinai/5466424 to your computer and use it in GitHub Desktop.
Save Vinai/5466424 to your computer and use it in GitHub Desktop.
Model & Resource Model Class Resolution Steps

Debugging (Resource) Model Instantiation

Follow each step manually and any bugs during (resource) model instantiation will become obvious.

Since many people find model and resource model instantiation to be one of the more challenging things in Magento 1, these are all the steps Magento does to resolve the factory name to the real PHP class name.

To debug, simply follow each step manually until you find a non-match. This works really well in my experience.
Less guessing, more and faster results.

In the examples I use a factory name of "example/thing".
This is split by Magento to a "class group" "example and a part-after-the-slash "thing".

Model Instantiation Steps

  1. Call to abstract factory method e.g. Mage::getModel('example/thing')
  2. Factory class name: example/thing
  3. Split into class group example and the rest thing
  4. Config xpath lookup: global/models/example/rewrite/thing
  5. If exists, use as class name. If not continue
  6. Config xpath lookup: global/models/example/class
  7. Resolves to model class prefix, e.g. This_Example_Model
  8. Append the part of the factory class name after the slash
  9. Resolves to This_Example_Model_thing
  10. Trigger autoloader which uppercases each word
  11. Resolves to file system path This/Example/Model/Resource/Thing.php
  12. Include this file and try to instantiate the class

Resource Model Instantiation Steps

  1. Call to abstract factory method, e.g. Mage::getResourceModel('example/thing')
  2. Factory class name: exampel/thing
  3. Split into class group example and the rest thing
  4. Config xpath lookup: global/models/example/resourceModel
  5. Resolves to "resource class group", e.g. example_resource
  6. Config xpath lookup: global/models/example_resource/rewrite/thing
  7. If exists, use as class name. If not continue
  8. Config xpath lookup: global/models/example_resource/deprecatedNode
  9. Resolves to former (pre Magento 1.6) resource class group, e.g. example_mysql4
  10. If it exists, config xpath lookup: global/models/example_mysql4/rewrite/thing
  11. If exists, use as class name. If not continue
  12. Config xpath lookup: global/models/example_resource/class
  13. Resolves to resource class prefix, e.g. This_Example_Model_Resource
  14. Append the part of the factory class name after the slash
  15. Resolves to This_Example_Model_Resource_thing
  16. Trigger autoloader which uppercases each word
  17. Resolves to file system path This/Example/Model/Resource/Thing.php
  18. Include this file and try to instantiate the class

Note that the purpose of the deprecatedNode lookup on step 8 is backward compatibility for old modules that rewrite resource models using the old, pre-Magento 1.6 resource class group names. There is a node literally called "deprecatedNode". (Magento introduced multi database support and major resource layer refactoring in 1.6 (MMBD)).

Note 2 that the same steps are true for instantiating collections, since they are also created using the Mage::getResourceModel() factory method.

Copy link

ghost commented Nov 11, 2015

changed the title from Pending commit check (from Codacy) is not displayed to Reviewable doesn't respect required status check settings from GitHub,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment