Place Magento model, resource, collection class at your will

Most Magento model, resource, collection are placed this way:

  • Model is located at Model folder
  • Resource is located at Model/Mysql4
  • Collection is located at Model/Mysql4/(ClassName)

What if you want organise them in a different way? Say how place all of 3 classes under one folder?

I enclose a complete a working sample.

1. Model class


class Qian_Msdk_Model_MediaGallery_Mg extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('msdk_mg/resource');
}
}

2. Resource class


class Qian_Msdk_Model_MediaGallery_Resource extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('msdk_mg/product_attribute_media_gallery', 'value_id');
}
}

3. Collection class


class Qian_Msdk_Model_MediaGallery_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('msdk_mg/mg');
}
}

4. config.xml


<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<models>
<msdk_mg>
<class>Qian_Msdk_Model_MediaGallery</class>
<resourceModel>msdk_mg_mysql4</resourceModel>
</msdk_mg>
<msdk_mg_mysql4>
<class>Qian_Msdk_Model_MediaGallery</class>
<entities>
<product_attribute_media_gallery>
<table>catalog_product_entity_media_gallery</table>
</product_attribute_media_gallery>
<product_attribute_media_gallery_value>
<table>catalog_product_entity_media_gallery_value</table>
</product_attribute_media_gallery_value>
</entities>
</msdk_mg_mysql4>
</models>
</global>
</config>

Leave a comment

Your email address will not be published. Required fields are marked *