In Part 3, we added a new table to the database with our install script. Now let’s create an EAV table setup so we can leverage Magento’s EAV data architecture setup.
If we are going to setup EAV tables, we will need a special kind of Setup class for our installation. So, we need to tell Magento to use a class we specify. For that, we will add a node to our config.xml.
1
2
3
4
5
6
7
8
9
10
11
|
<!– app/code/local/Namespace/Modulename/etc/config.xml –>
…
<global>
<resources>
<modulename_setup>
<module>Namespace_Modulename</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</modulename_setup>
</resources>
</global>
...
|
The <class> node lets Magento know that this is what class we want to leverage in our install files. For this install file, we are going to use Mage_Eav_Model_Entity_Setup . So let’s start building our new entity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
// app/code/local/Namespace/Modulename/sql/modulename_setup/install-1.0.0.php
$installer = $this;
$installer->startSetup();
$installer->addEntityType(‘modulename_content’, array(
‘entity_model’ => ‘modulename/content’,
‘table’ => ‘modulename/content’
));
$installer->createEntityTables($installer->getTable(‘modulename/content’));
$installer->endSetup();
|
OK, so what in the world just happened? Let’s go through it.
1
2
3
4
|
$installer->addEntityType(‘modulename_content’, array(
‘entity_model’ => ‘modulename/content’,
‘table’ => ‘modulename/content’
));
|
What this did was create a new row in the eav_entity_type
table. Now Magento knows that you have a new EAV table set to work with. The ‘entity_model’ is the model you will be working with using the standard modelAbstract Factory Pattern. The ‘table’ reference is the table you designated in the config.xml file. (See Part 3 for reference).
1
|
$installer->createEntityTables($installer->getTable(‘modulename/content’));
|
What this did was create all of your tables for you. That’s right! All of your tables in one handy little line! It creates:
- content (base table)
- content_char
- content_datetime
- content_decimal
- content_int
- content_text
- content_varchar
Now you have your entity tables ready to go! Many people think that creating EAV is tough, but Magento does all the heavy lifting for you. While this isn’t the only part that goes into creating full fledged EAV models, the next lesson goes into creating attributes.
Overview : Magento Installation Scripts (Cool Ryan)
Previous :Part 3: Installing New Tables
Next : Part 5: Attributes
Post from coolryan.com