Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I know how to use a resource, but I'm not sure what it is. All I understand is that I put in in my XML file and it makes my application work. Can someone explain in simple terms what it is I'm doing when I put it in my XML file?

    <resources>
        <complexworld_setup>
            <setup>
                <module>Magentotutorial_Complexworld</module>
                <class>Magentotutorial_Complexworld_Model_Resource_Setup</class>
            </setup>
        </complexworld_setup>
    </resources>

and

    <complexworld_resource>
        <class>Magentotutorial_Complexworld_Model_Resource</class>
        <entities>
            <eavblogpost>
                <table>eavblog_posts</table>
            </eavblogpost>
        </entities>
    </complexworld_resource>
share|improve this question
1  
Please describe what you mean by resource. What do you put in your XML? There is more than one node with the term resource in it :) –  Fabian Blechschmidt May 27 at 12:31
    
@Fabian Edited :) –  Kyle P May 27 at 13:22
    
Great, then the second part is answered by Marius. This is the definiton of the class prefix of the resource models and the name of one table (there can be more) –  Fabian Blechschmidt May 27 at 13:31

2 Answers 2

up vote 2 down vote accepted

he first part is about magentos setup/data install system.

Every module wants to do "things" in the system. Install new Attributes, create new tables, etc. Create products for special purpose, ...

All this can be done with the magento setup scripts. They are living in app/code/<community|local>/<Namespace>/<Modulname>/<sql|data>/<setupname>/.

I hope you know what community, local, Namespace and Modulname is, so I won't explain it.

Magento has two different types of setup scripts:

sql

sql scripts are living in sql and used to do changes to the database, like adding new columns, creating and dropping indexes, creating new attributes, etc. They run pretty early, especially before the store system is initialized

data

data scripts are living in data and rund later, so you already know, that there are websites and stores exist.

setupname

In your example, the setupname would be complexworld_setup, because this is the XML node name.

naming

The names of the scripts are:

(data-)install-<version>.php
(data-)upgrade-<from_version>-<to_version>.php

data scripts are prefixed with data, the sql scripts omit this prefix.

class context

The setup scripts run in the context of the <class> you sepcify, if you omit the class, the default is Mage_Core_Model_Resource_Setup. You have to declare a class at least for changes to the different EAV types (customer, product, category, ...) and for changes to the no longer EAV types (quote, order, order_item, ...).

I still don't know exactly why this node exists. And what it is for. Rico Neitzel told me, that because all the config is merged in a big xml structure, you would later now know, which setup belongs to which module.

  • Thanks, the penny has dropped.

In the context of the setup script you need to know the module, because otherwise magento has no idea, to update from which version to which version.

I hope this helps a bit to understand...

share|improve this answer
    
Thanks that was really helpful. Especially the last bit about the class context. –  Kyle P May 27 at 14:56

A resource model is a connector between the model and the database.
Models that are mapped to a table (or more in the EAV case) should not talk directly to the database. They should have a resource model associated that does the "dirty work" of inserting/updating/deleting/retrieving data from the tables.

Models that are not mapped to a table (like Observers) don't have such a resource model.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.