Magento EAV Entity – How to Create an EAV Entity

admineavmodule

How to create an EAV entity?

This is a question that pops up a lot on the web. There are some good blog articles that explain how to do that, but none of them are satisfying for me.
So I decided to a self-answered question and explain how I do it … and it seems to work.

There is a lot of code in here. In order to read correctly sort the answers by 'oldest'.

Best Answer

Part 1

For the purpose of this demo I'm going to create a module, let's name it Easylife_News that contains an entity named Article.
The module comes with complete CRUD code for backend, including the section for managing attributes, and some simple frontend listing and view page for each article. It also ads a menu item in the top menu to the listing of articles.
It also contains URL rewrites, RSS feed and breadcrumbs for frontend. This entity will have 4 attributes: Title, Short Description, Description, Publish Date in addition to the system ones (created_at, updated_at, status, in_rss, url_key).
The module should contain the following files.

app/etc/module/Easylife_News.xml - the declaration module:

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_News>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog /><!-- some classes extend one class from the catalog module to avoid duplicating it -->
             </depends>
        </Easylife_News>
    </modules>
</config>

app/code/local/Easylife/News/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_News>
            <version>1.0.0</version>
        </Easylife_News>
    </modules>
    <global>
        <resources>
            <easylife_news_setup>
                <setup>
                    <module>Easylife_News</module>
                    <class>Easylife_News_Model_Resource_Setup</class>
                </setup>
            </easylife_news_setup>
        </resources>
        <blocks>
            <easylife_news>
                <class>Easylife_News_Block</class>
            </easylife_news>
        </blocks>
        <helpers>
            <easylife_news>
                <class>Easylife_News_Helper</class>
            </easylife_news>
        </helpers>
        <models>
            <easylife_news>
                <class>Easylife_News_Model</class>
                <resourceModel>easylife_news_resource</resourceModel>
            </easylife_news>
            <easylife_news_resource>
                <class>Easylife_News_Model_Resource</class>
                <entities>
                    <article>
                        <table>easylife_news_article</table>
                    </article>
                    <eav_attribute>
                        <table>easylife_news_eav_attribute</table>
                    </eav_attribute>
                </entities>
            </easylife_news_resource>
        </models>
        <events>
            <controller_front_init_routers><!-- event for custom router - url rewrites -->
                <observers>
                    <easylife_news>
                        <class>Easylife_News_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </easylife_news>
                </observers>
            </controller_front_init_routers>
        </events>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <easylife_news>
                    <file>easylife_news.xml</file>
                </easylife_news>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easylife_News>
                    <files>
                        <default>Easylife_News.csv</default>
                    </files>
                </Easylife_News>
            </modules>
        </translate>
        <global_search>
            <article>
                <class>easylife_news/adminhtml_search_article</class>
                <acl>easylife_news</acl>
            </article>
        </global_search>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Easylife_News before="Mage_Adminhtml">Easylife_News_Adminhtml</Easylife_News>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <frontend>
        <events>
            <page_block_html_topmenu_gethtml_before><!-- add link to top menu -->
                <observers>
                    <easylife_news>
                        <class>easylife_news/observer</class>
                        <method>addItemsToTopmenuItems</method>
                    </easylife_news>
                </observers>
            </page_block_html_topmenu_gethtml_before>
        </events>

        <routers>
            <easylife_news>
                <use>standard</use>
                <args>
                    <module>Easylife_News</module>
                    <frontName>news</frontName>
                </args>
            </easylife_news>
        </routers>
        <layout>
            <updates>
                <easylife_news>
                    <file>easylife_news.xml</file>
                </easylife_news>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easylife_News>
                    <files>
                        <default>Easylife_News.csv</default>
                    </files>
                </Easylife_News>
            </modules>
        </translate>
    </frontend>
    <default>
        <easylife_news>
            <article>
                <breadcrumbs>1</breadcrumbs>
                <url_prefix>article</url_prefix>
                <url_suffix>html</url_suffix>
                <rss>1</rss>
                <meta_title>Articles</meta_title>
            </article>
        </easylife_news>
    </default>
</config>

app/code/local/Easylife/News/etc/adminhtml.xml - the admin acl and menu file.

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <easylife_news translate="title" module="easylife_news">
                                        <title>News</title>
                                    </easylife_news>
                                </children>
                            </config>
                        </children>
                    </system>
                    <cms>
                        <children>
                            <easylife_news translate="title" module="easylife_news">
                                <title>News</title>
                                    <children>
                                        <article translate="title" module="easylife_news">
                                            <title>Article</title>
                                            <sort_order>0</sort_order>
                                        </article>
                                        <article_attributes translate="title" module="easylife_news">
                                            <title>Manage Article attributes</title>
                                            <sort_order>7</sort_order>
                                        </article_attributes>
                                    </children>
                                </easylife_news>
                            </children>
                        </cms>

                </children>
            </admin>
        </resources>
    </acl>
    <menu>
        <cms><!-- I added the admin menu under CMS. Feel free to move it. -->
            <children>
                <easylife_news translate="title" module="easylife_news">
                    <title>News</title>
                    <sort_order>17</sort_order>
                    <children>
                        <article translate="title" module="easylife_news">
                            <title>Article</title>
                            <action>adminhtml/news_article</action>
                            <sort_order>0</sort_order>
                        </article>
                        <article_attributes translate="title" module="easylife_news">
                            <title>Manage Article Attributes</title>
                            <action>adminhtml/news_article_attribute</action>
                            <sort_order>7</sort_order>
                        </article_attributes>
                    </children>
                </easylife_news>
            </children>
        </cms>

    </menu>
</config>

app/code/local/Easylife/News/etc/system.xml - the system configuration file. It allows you to manage a few settings like SEO for the articles list, breadcrumbs, RSS enable/disable, url prefix and suffix

<?xml version="1.0"?>
<config>
    <tabs>
        <easylife translate="label" module="easylife_news">
            <label>News</label>
            <sort_order>2000</sort_order>
        </easylife>
    </tabs>
    <sections>
        <easylife_news translate="label" module="easylife_news">
            <class>separator-top</class>
            <label>News</label>
            <tab>easylife</tab>
            <frontend_type>text</frontend_type>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <article translate="label" module="easylife_news">
                    <label>Article</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>0</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <breadcrumbs translate="label">
                            <label>Use Breadcrumbs</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </breadcrumbs>
                        <url_prefix translate="label comment">
                            <label>URL prefix</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Leave empty for no prefix</comment>
                        </url_prefix>
                        <url_suffix translate="label comment">
                            <label>Url suffix</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>What goes after the dot. Leave empty for no suffix.</comment>
                        </url_suffix>
                        <rss translate="label">
                            <label>Enable rss</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>40</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </rss>
                        <meta_title translate="label">
                            <label>Meta title for articles list page</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>50</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </meta_title>
                        <meta_description translate="label">
                            <label>Meta description for articles list page</label>
                            <frontend_type>textarea</frontend_type>
                            <sort_order>60</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </meta_description>
                        <meta_keywords translate="label">
                            <label>Meta keywords for articles list page</label>
                            <frontend_type>textarea</frontend_type>
                            <sort_order>70</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </meta_keywords>
                    </fields>
                </article>
            </groups>
        </easylife_news>
    </sections>
</config>

app/code/local/Easylife/News/sql/easylife_news_setup/install-1.0.0.php - the install script

<?php
$this->startSetup();
//create the entity table
$table = $this->getConnection()
    ->newTable($this->getTable('easylife_news/article'))
    ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Entity ID')
    ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
        ), 'Entity Type ID')
    ->addColumn('attribute_set_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
        ), 'Attribute Set ID')
    ->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
        ), 'Creation Time')
    ->addColumn('updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
        ), 'Update Time')
    ->addIndex($this->getIdxName('easylife_news/article', array('entity_type_id')),
        array('entity_type_id'))
    ->addIndex($this->getIdxName('easylife_news/article', array('attribute_set_id')),
        array('attribute_set_id'))
    ->addForeignKey(
        $this->getFkName(
            'easylife_news/article',
            'attribute_set_id',
            'eav/attribute_set',
            'attribute_set_id'
        ),
        'attribute_set_id', $this->getTable('eav/attribute_set'), 'attribute_set_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
    ->addForeignKey($this->getFkName('easylife_news/article', 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
        'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
    ->setComment('Article Table');
$this->getConnection()->createTable($table);

//create the attribute values tables (int, decimal, varchar, text, datetime)
$articleEav = array();
$articleEav['int'] = array(
    'type'      => Varien_Db_Ddl_Table::TYPE_INTEGER,
    'length'    => null,
    'comment'   => 'Article Datetime Attribute Backend Table'
);

$articleEav['varchar'] = array(
    'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length'    => 255,
    'comment'   => 'Article Varchar Attribute Backend Table'
);

$articleEav['text'] = array(
    'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length'    => '64k',
    'comment'   => 'Article Text Attribute Backend Table'
);

$articleEav['datetime'] = array(
    'type'      => Varien_Db_Ddl_Table::TYPE_DATETIME,
    'length'    => null,
    'comment'   => 'Article Datetime Attribute Backend Table'
);

$articleEav['decimal'] = array(
    'type'      => Varien_Db_Ddl_Table::TYPE_DECIMAL,
    'length'    => '12,4',
    'comment'   => 'Article Datetime Attribute Backend Table'
);

foreach ($articleEav as $type => $options) {
    $table = $this->getConnection()
        ->newTable($this->getTable(array('easylife_news/article', $type)))
        ->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'identity'  => true,
            'nullable'  => false,
            'primary'   => true,
            ), 'Value ID')
        ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
            ), 'Entity Type ID')
        ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
            ), 'Attribute ID')
        ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
            ), 'Store ID')
        ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
            ), 'Entity ID')
        ->addColumn('value', $options['type'], $options['length'], array(
            ), 'Value')
        ->addIndex(
            $this->getIdxName(
                array('easylife_news/article', $type),
                array('entity_id', 'attribute_id', 'store_id'),
                Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE
            ),
            array('entity_id', 'attribute_id', 'store_id'),
            array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE))
        ->addIndex($this->getIdxName(array('easylife_news/article', $type), array('store_id')),
            array('store_id'))
        ->addIndex($this->getIdxName(array('easylife_news/article', $type), array('entity_id')),
            array('entity_id'))
        ->addIndex($this->getIdxName(array('easylife_news/article', $type), array('attribute_id')),
            array('attribute_id'))
        ->addForeignKey(
            $this->getFkName(
                array('easylife_news/article', $type),
                'attribute_id',
                'eav/attribute',
                'attribute_id'
            ),
            'attribute_id', $this->getTable('eav/attribute'), 'attribute_id',
            Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
        ->addForeignKey(
            $this->getFkName(
                array('easylife_news/article', $type),
                'entity_id',
                'easylife_news/article',
                'entity_id'
            ),
            'entity_id', $this->getTable('easylife_news/article'), 'entity_id',
            Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
        ->addForeignKey($this->getFkName(array('easylife_news/article', $type), 'store_id', 'core/store', 'store_id'),
            'store_id', $this->getTable('core/store'), 'store_id',
            Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
        ->setComment($options['comment']);
    $this->getConnection()->createTable($table);
}
//crete the news_eav_attribute (for additional attribute settings)
$table = $this->getConnection()
    ->newTable($this->getTable('easylife_news/eav_attribute'))
    ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Attribute ID')
    ->addColumn('is_global', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute scope')
    ->addColumn('position', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute position')
    ->addColumn('is_wysiwyg_enabled', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute uses WYSIWYG')
    ->addColumn('is_visible', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute is visible')
    ->setComment('News attribute table');
$this->getConnection()->createTable($table);

$this->installEntities();
$this->endSetup();

Models app/code/local/Easylife/News/Model/Observer.php - add the new menu item on the top menu:

<?php
class Easylife_News_Model_Observer {
    public function addItemsToTopmenuItems($observer) {
        $menu = $observer->getMenu();
        $tree = $menu->getTree();
        $action = Mage::app()->getFrontController()->getAction()->getFullActionName();

        $articleNodeId = 'article';
        $data = array(
            'name' => Mage::helper('easylife_news')->__('Articles'),
            'id' => $articleNodeId,
            'url' => Mage::helper('easylife_news/article')->getArticlesUrl(),
            'is_active' => ($action == 'easylife_news_article_index' || $action == 'easylife_news_article_view')
        );
        $articleNode = new Varien_Data_Tree_Node($data, 'id', $tree, $menu);
        $menu->addChild($articleNode);
        return $this;
    }
}

app/code/local/Easylife/News/Model/Article.php - the article main model

<?php
class Easylife_News_Model_Article
    extends Mage_Catalog_Model_Abstract {
    const ENTITY    = 'easylife_news_article';
    const CACHE_TAG = 'easylife_news_article';
    protected $_eventPrefix = 'easylife_news_article';
    protected $_eventObject = 'article';
    public function _construct(){
        parent::_construct();
        $this->_init('easylife_news/article');
    }
    protected function _beforeSave(){
        parent::_beforeSave();
        $now = Mage::getSingleton('core/date')->gmtDate();
        if ($this->isObjectNew()){
            $this->setCreatedAt($now);
        }
        $this->setUpdatedAt($now);
        return $this;
    }
    public function getArticleUrl(){
        if ($this->getUrlKey()){
            $urlKey = '';
            if ($prefix = Mage::getStoreConfig('easylife_news/article/url_prefix')){
                $urlKey .= $prefix.'/';
            }
            $urlKey .= $this->getUrlKey();
            if ($suffix = Mage::getStoreConfig('easylife_news/article/url_suffix')){
                $urlKey .= '.'.$suffix;
            }
            return Mage::getUrl('', array('_direct'=>$urlKey));
        }
        return Mage::getUrl('easylife_news/article/view', array('id'=>$this->getId()));
    }
    public function checkUrlKey($urlKey, $active = true){
        return $this->_getResource()->checkUrlKey($urlKey, $active);
    }

    public function getDescription(){ //this needs to be implemented for all WYSIWYG attributes
        $description = $this->getData('description');
        $helper = Mage::helper('cms');
        $processor = $helper->getBlockTemplateProcessor();
        $html = $processor->filter($description);
        return $html;
    }
    public function getDefaultAttributeSetId() {
        return $this->getResource()->getEntityType()->getDefaultAttributeSetId();
    }
    public function getAttributeText($attributeCode) {
        $text = $this->getResource()
            ->getAttribute($attributeCode)
            ->getSource()
            ->getOptionText($this->getData($attributeCode));
        if (is_array($text)){
            return implode(', ',$text);
        }
        return $text;
    }
    public function getDefaultValues() {
        $values = array();
        $values['status'] = 1;
        $values['in_rss'] = 1;
        return $values;
    }
}

app/code/local/Easylife/News/Model/Resource/Article.php - the resource model for the article

<?php
class Easylife_News_Model_Resource_Article
    extends Mage_Catalog_Model_Resource_Abstract {
    public function __construct() {
        $resource = Mage::getSingleton('core/resource');
        $this->setType('easylife_news_article')
            ->setConnection(
                $resource->getConnection('article_read'),
                $resource->getConnection('article_write')
            );

    }
    public function getMainTable() {
        return $this->getEntityTable();
    }
    public function checkUrlKey($urlKey, $storeId, $active = true){
        $stores = array(Mage_Core_Model_App::ADMIN_STORE_ID, $storeId);
        $select = $this->_initCheckUrlKeySelect($urlKey, $stores);
        if (!$select){
            return false;
        }
        $select->reset(Zend_Db_Select::COLUMNS)
            ->columns('e.entity_id')
            ->limit(1);
        return $this->_getReadAdapter()->fetchOne($select);
    }
    protected function _initCheckUrlKeySelect($urlKey, $store){
        $urlRewrite = Mage::getModel('eav/config')->getAttribute('easylife_news_article', 'url_key');
        if (!$urlRewrite || !$urlRewrite->getId()){
            return false;
        }
        $table = $urlRewrite->getBackend()->getTable();
        $select = $this->_getReadAdapter()->select()
            ->from(array('e' => $table))
            ->where('e.attribute_id = ?', $urlRewrite->getId())
            ->where('e.value = ?', $urlKey)
            ->where('e.store_id IN (?)', $store)
            ->order('e.store_id DESC');
        return $select;
    }
}

app/code/local/Easylife/News/Model/Resource/Article/Collection.php - the collection model

<?php
class Easylife_News_Model_Resource_Article_Collection
    extends Mage_Catalog_Model_Resource_Collection_Abstract {
    protected function _construct(){
        parent::_construct();
        $this->_init('easylife_news/article');
    }
    protected function _toOptionArray($valueField='entity_id', $labelField='title', $additional=array()){
        $this->addAttributeToSelect('title');
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }
    protected function _toOptionHash($valueField='entity_id', $labelField='title'){
        $this->addAttributeToSelect('title');
        return parent::_toOptionHash($valueField, $labelField);
    }
    public function getSelectCountSql(){
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(Zend_Db_Select::GROUP);
        return $countSelect;
    }
}

The code is too long to fit in one answer. The rest will follow....wait for it.

Related Topic