1
votes

I am trying to understand syntax for Magento ORM data base access. Can someone help explain the pieces used in this XML config file.

    <models>
        <modulex>
            <class>MynameSpace_Modulex_Model</class>
            <resourceModel>modulex_mysql4</resourceModel>
        </modulex>   

        <modulex_mysql4>
            <class>MynameSpace_Modulex_Model_Mysql4</class>              
            <entities>
                <blogpost>
                    <table>my_blog_posts</table>
                </blogpost>
            </entities>                 
        </modulex_mysql4>        
    </models>    
  • models - section used to define classes related to database access & business logic

  • modulex - name of a specific module; Stuff contained within this section is only applicable to module named modulex

  • resourceModel - indicates a directory or points to XML entity to follow? specific for database access?

  • modulex_mysql4 - definition of resourceModel used above? why not just put this within modulex section?

  • class MynameSpace_Modulex_Model - points to location of classes in this module? makes instance of the class?

  • entities - define resource models in my module

  • blogpost - name of model (both the file and class in MynameSpace/Modulex/Model)

  • my_blog_posts - name of sql table.

2

2 Answers

3
votes

There is a big difference between Model and Resource model

  • Models - Handles business logic (in any MVC)
  • Resource Models - Handles database interaction

Explaining your example

  • Modulex - Model name
  • MynameSpace_Modulex_Model - Yes, path to your module's model directory. It is only instantiated when firing query, such as: Mage::getModel('modulex/modulex')->load(1); But if you simply use Mage::getModel('modulex/modulex'); it will not instantiate because Magento believes in Lazy Loading.
  • Entities - Represents which tables will be used for this module. It is also used to get the table name for particular entity of the module. (see example below)
  • blogpost - No, it's not your model name. Your model name is Modulex. blogpost refers to your entity table name for table my_blog_posts (see example below) my_blog_posts - yes, it's name of your database table
  • mysql4 - As of community edition 1.6, mysql4 is deprecated and now used as Resource instead. Resources are used to interact database directly.

Example use of Model and Entity table name:

$table = Mage::getSingleton('core/resource')->getTableName('modulex/blogpost');

Here, modulex = your model, blogpost = your entity table name. Both are used to get the database table name.

1
votes

"As of community edition 1.6, mysql4 is deprecated and now used as Resource instead. Resources are used to interact database directly."

If I understand the change, then config.xml should look more like this in the future...

...
<global>
  <models>
    <modulex>
      <class>Msc_Modulex_Model</class>
      <resourceModel>modulex_resource</resourceModel>
    </modulex>

<!--    <modulex_mysql4>                            -->
<!--        <class>MynameSpace_Modulex_Model_Mysql4</class>    -->
<!--        <entities>                                -->
<!--            <blogpost>                            -->
<!--                <table>my_blog_posts</table>    -->
<!--            </blogpost>                            -->
<!--        </entities>                             -->
<!--    </modulex_mysql4>                            -->

    <modulex_resource>
      <class>MynameSpace_Modulex_Model_Resource</class>
      <deprecatedNode>modulex_mysql4</deprecatedNode>
      <entities>
        <blogpost>
           <table>my_blog_posts</table>
        </blogpost>
      </entities>
    </modulex_resource>
  </models>
</global>
...

also corresponding change in Blogpost.php

<?php
// in \app\code\local\Msc\Module5\Model\Mysql4\Blogpost.php
//class Msc_Modulex_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract{
class Msc_Modulex_Model_Resource_Db_Blogpost extends Mage_Core_Model_Resource_Db_Abstract{
    protected function _construct()
    {
        $this->_init('modulex/blogpost', 'my_blogpost_id');
    }   
}