Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Custom Resource Model
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Hi all,

I would like to know if someone has created a custom module with custom resource models.

I can create a db object with the following:

// fetch write database connection that is used in Mage_Core module
$write Mage::getSingleton('core/resource')->getConnection('core_write');

But I would really like to create a resource model, patterned after the Mage_Core modules. For example, the config.xml file for Mage/Catalog contains the following:

<global>
      <
models>
         <
catalog>
            <class>
Mage_Catalog_Model</class>
            <
resourceModel>catalog_entity</resourceModel>
         </
catalog>
...

I can see that this corresponds to Mage/Catalog/Model/Entity. I am just not making a connection from the code to the config.

Herein lies my confusion: I see the configuration in the config.xml, and I can look at the class models for this module.

so,

1) How can I build a resource model class that will extend the core/write functionality? aka specific table etc.

2) How can I reference the resource model class from a model class?

I can feel I am getting close to figuring this out. Any help is greatly appreciated.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tman_f
Sr. Member
 
Total Posts:  85
Joined:  2008-02-12
 

Basically I am looking for someone who can walk me through creating a new module that includes a resource model. For testing purpose, lets call the new module local/Mage/TestMod.

And for this instance, lets say I need access to the product db tables. To get specific, lets say I want to grab a product’s attribute, and write a new value to the db.

Appreciate the help.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
beowax
Sr. Member
 
Total Posts:  94
Joined:  2007-12-17
Lille, France
 

tman,

Have you more informations on overriding a model ?

Regards,
Antoine

 
Magento Community Magento Community
Magento Community
Magento Community
 
Matt Kammersell
Jr. Member
 
Avatar
Total Posts:  16
Joined:  2007-09-21
Las Vegas, NV
 

I’m interested as well, would LOVE documentation on creating your own tables.

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Any progress on this area by anyone?  This is a huge deal for me and I have been banging my head against the wall trying to figure out how it works but I haven’t made much progress.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
beowax
Sr. Member
 
Total Posts:  94
Joined:  2007-12-17
Lille, France
 

I found a solution :

1. I overrides a block,
2. and a controller,
3. In these i work with my custom model, which extends the previous one…

I don’t kwnow if its the right way to do, but it works quite well. wink

Regards,
A.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

how did you overwrite a controller? can you please give me an example? i tried to overwrite saveOrderAction to customize for my needs but it didnt work. Did you “rewrite\” your controller?

cheers

Stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
beowax
Sr. Member
 
Total Posts:  94
Joined:  2007-12-17
Lille, France
 

I should have choose my words…

I didn’t rewrite the controller, only the block, which calls my custom controller instead of the “Mage” one wink

Regards,
A.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

also a good idea… Ill try this smile

cheers

stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
glaDiator
Member
 
Avatar
Total Posts:  65
Joined:  2008-03-17
 

Hi,
If anyone found the solution ... please paste it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 
averhelst - 06 April 2008 11:57 PM

I should have choose my words…

I didn’t rewrite the controller, only the block, which calls my custom controller instead of the “Mage” one wink

Regards,
A.

Hui,

thanks that works smile!

cheers

stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Hi guys,

I can kind of see what you are doing, I am still trying to wrap my head around all of this.  For your new module and creating a database to go along with that module did you have to create your own database tables or use the XML setup tag in config.xml and create the mysql4 directories and folders?  Also when you create the table i.e. ‘dealer_stores’ in mysql how do you specifically access that in the new module.  I see this for example in the block of the Tags module:

protected function _getCollection()
{
if( !$this->_collection && $this->getProductId() ) {

$model = Mage::getModel(’tag/tag’);
$this->_collection = $model->getResourceCollection()
->addPopularity()
->addStatusFilter($model->getApprovedStatus())
->addProductFilter($this->getProductId())
->addStoreFilter(Mage::app()->getStore()->getId())
->setActiveFilter()
->load();
}
return $this->_collection;
}

Now from what I am understanding about the EAV module and the Magento is that you don’t actually have to hardcode in the SQL queries as it will automagically figure out what table and fields to select from the table based on the model and class.  I.E.

1) set the model equal to tag/tag -> tag/Model/tag.php (?)
2) into protected array _collection using the model load a resource collection
3) addPopularity, where is this defined...is this understood because there is a database field named popularity in tag_summary
4) addStatusFilter, since the ‘tag’ database table has a status field and tags must be approved first, does this function automaticallly look for that ‘status’ field to load the status of whatever model it is given?
5) addProductFilter, does this load the current product that you are looking at or does it look at the product_id field in ‘tag_relation’ or does it look at ‘tag_summary’ at the products field, though I wouldn’t think that one
6) addStoreFilter, self-explanatory...it just looks to the current store and whatever one you are on and adds that to the select query statement
7) setActiveFilter, does this look to the ‘tag_relation’ table and the active field and if active displays those?
8) load must take the database query results and put it into the _collection array.

Any help with my thinking and questions?

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
glaDiator
Member
 
Avatar
Total Posts:  65
Joined:  2008-03-17
 

Thanks alistek .............. these will help lot.......anyone to answer the questions.........?

 
Magento Community Magento Community
Magento Community
Magento Community
 
sbditto85
Jr. Member
 
Total Posts:  6
Joined:  2010-05-18
 

I wanted to overwrite the ‘sitemap/catalog_product’ resource to make some modifications on how the URL was retrieved.  So I overwrote

Mage_Sitemap_Model_Mysql4_Catalog_Product

by adding this to my custom modules config.xml

<global>
        <
models>
            <
sitemap_mysql4>
                <
rewrite>
                    <
catalog_product>NAME_OF_NEW_CLASS</catalog_product>
                </
rewrite>
            </
sitemap_mysql4>
        </
models>
    </global>

Hope that helps.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top