Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dfelton/8ca84c94ee0b57e346a3e7469d814214 to your computer and use it in GitHub Desktop.
Save dfelton/8ca84c94ee0b57e346a3e7469d814214 to your computer and use it in GitHub Desktop.
Magento - Quick Reference Guide for creating new attributes via an installer script for EAV and flat table entity types.
<?php
/**
* Flat tables entity types will most likely not automatically render
* your new table column within the admin form for the entity. If
* you would like an admin form to be created for them reference this:
* @see https://gist.github.com/dfelton/8ca84c94ee0b57e346a3e7469d814214#file-200-form_fileds.php
*/
// IMPORTANT: Magento heavily caches the table descrptions. After
// creating your new columns you will probably have to blow out the
// "var/cache" directory before Magento will recognize that the new
// column exists.
// This file should be located here, replace the capital words with appropriate values:
// FILE: app/code/CODE_POOL/MODULE_NAMESPACE/MODULE_NAME/sql/IDENTIFIER/upgrade-OLD_VERSION-NEW_VERSION.php
// To have this file run, update the <version> of your extension in
// your extensions 'etc/config.xml' file (if caching is turned on
// you'll need to flush the CONFIG cache type).
// Most all setup models, regardless of your extension, probably extend this class already.
/* @var $this Mage_Core_Model_Resource_Setup */
$this->startSetup();
// Next, copy/paste individual pieces from below for the fields you need.
// Make sure to remember to include $this->endSetup() at the end of your file.
// Below, whenever $this->getTable('IDENTIFIER/ENTITY') is referenced,
// it should be replaced with the same string used to fetch the model
// you are looking to modify the table for. For example, to add
// to the table used for Mage::getModel('cms/page'), we'd replace
// $this->getTable('IDENTIFIER/ENTITY') with $this->getTable('cms/page').
// Add INTEGER column
// Appropriate for:
// - Relational column with other entities
// - YES/NO select dropdowns
// - single value select dropdowns (which store integer values)
// - sort_order field
$this
->getConnection()
->addColumn(
$this->getTable('IDENTIFIER/ENTITY'), // <!-- Change this
'attribute_code', // <!-- Change This
array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
'length' => 11, // <!-- Change if appropriate, for YES/NO "1" would be better
'nullable' => true, // <!-- Change if desired
'default' => null,
'comment' => 'Comment' // <!-- Change this (use your planned label value)
)
)
;
// VARCHAR
// Appropriate for:
// - input type: text
// - single values select dropdowns (which store strings values over integers)
// - multi-select dropdowns (store values as CSV)
// - image fields
$this
->getConnection()
->addColumn(
$this->getTable('IDENTIFIER/ENTITY'), // <!-- Change this
'attribute_code', // <!-- Change this
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => '255', // <!-- 255 is max for varchar. Higher value will automatically create TEXT column type.
'nullable' => true, // <!-- Change if desired
'default' => null, // <!-- Change if desired
'comment' => 'Comment' // <!-- Change this (use your planned label value)
)
)
;
$this->endSetup();
<?php
// TODO: start this
<?php
// This file should be located here, replace the capital words with appropriate values:
// FILE: app/code/CODE_POOL/MODULE_NAMESPACE/MODULE_NAME/sql/IDENTIFIER/upgrade-OLD_VERSION-NEW_VERSION.php
// To have this file run, update the <version> of your extension in your extensions 'etc/config.xml' file.
// If your installer script is already an instance of Mage_Catalog_Model_Resource_Setup
// you can simply use "$this" in replacement of "$setup"
/* @var $setup Mage_Catalog_Model_Resource_Setup */
$setup = Mage::getModel('catalog/resource_setup', 'core_setup');
// Type: Image
$setup->addAttribute(
Mage_Catalog_Model_Category::ENTITY,
'attribute_code', // <!-- Change this
array(
'type' => 'varchar',
'label' => 'Attribute Label', // <!-- Change this
'input' => 'image',
'backend' => 'catalog/category_attribute_backend_image',
'required' => false, // <!-- Need to confirm but I don't think it's possible to make image fields required anyway. Default is true though so supplying here for clarity
'sort_order' => 100, // <!-- Change this if desired
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'group' => 'General Information', // If a new tab is specified that tab will be created automatically
)
);
// Type: Yes/No
$setup->addAttribute(
Mage_Catalog_Model_Category::ENTITY,
'attribute_code', // <!-- Change This
array(
'input' => 'select',
'type' => 'int',
'source' => 'eav/entity_attribute_source_boolean',
'label' => 'Attribute Label', // <!-- Change this
'required' => 0,
'sort_order' => 100, // <!-- Change this if desired
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'group' => 'General Information', // If a new tab is specified that tab will be created automatically
)
);
// Type: Select Dropdown
$setup->addAttribute(
Mage_Catalog_Model_Category::ENTITY,
'attribute_code', // <!-- Change This
array(
'input' => 'select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'group' => 'General Information', // <!-- Change if desired
'label' => 'Attribute Label', // <!-- Change this
'sort_order' => 100, // <!-- Change if desired
// @see https://gist.github.com/dfelton/8ca84c94ee0b57e346a3e7469d814214#file-300-select-php
// Should ideally rename the class name to reflect the attribute code it serves.
'source' => 'firstscribe_moduleName/attribute_source_type_select',
)
);
// Type: WYSIWYG
$setup->addAttribute(
Mage_Catalog_Model_Category::ENTITY,
'attribute_code', // <!-- Change this
array(
'input' => 'textarea',
'type' => 'text',
'label' => 'Attribute Label', // <!-- Change this
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'group' => 'General Information', // <!-- Change this if desired
'sort_order' => null, // <!-- Change this if desired
'required' => false, // <!-- Change this if desired
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
)
);
// Default Configuration Option Values
/** @see Mage_Catalog_Model_Resource_Setup::_prepareValues */
/** @see Mage_Eav_Model_Entity_Setup::_prepareValues */
// Attribute which only apply to products have been ommitted
$defaultOptions = array(
'backend' => null,
'default' => null,
'frontend' => null,
'frontend_class' => null,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // STORE = 0; GLOBAL = 1; WEBSITE = 2
'input' => 'text', // button|checkbox|checkboxes|collection|column|date|editor|fieldset|file|gallery|hidden|image|imagefile|label|link|multiline|multiselect|note|obsure|password|radio|radios|reset|select|submit|text|textarea|time
'input_renderer' => null,
'is_html_allowed_on_front' => 0,
'label' => null,
'note' => null,
'position' => 0,
'required' => 1, // 1|0
'source' => null,
'table' => null,
'type' => 'varchar', // datetime|decimal|int|text|varchar
'unique' => 0,
'visible' => 1, // TODO: Does this really apply to categories or only products?
'visible_on_front' => 0, // TODO: Does this really apply to categories or only products?
'wysiwyg_enabled' => 0,
);
<?php
$this->startSetup();
/* @var $setup Mage_Catalog_Model_Resource_Setup */
/* @var $category Mage_Catalog_Model_Category */
$setup = Mage::getModel('catalog/resource_setup', 'core_setup');
$category = Mage::getModel('catalog/category');
$setId = $category->getDefaultAttributeSetId();
$setup->addAttributeGroup(Mage_Catalog_Model_Category::ENTITY, $setId, 'Meta Information', 25);
$groupId = $setup->getAttributeGroupId(Mage_Catalog_Model_Category::ENTITY, $setId, 'Meta Information');
foreach (array('meta_title','meta_keywords','meta_description') as $attributeCode) {
$setup->addAttributeToGroup(Mage_Catalog_Model_Category::ENTITY, $setId, $groupId, $attributeCode);
}
$this->endSetup();
<?php
// IMPORTANT: This file may already exist if it is part of your own extension.
// If this is the case, just add to your _prepareForm() method.
// If this is for another module's entity type, you may have to do the extra work
// to create your own tab.
// This file should be located here, replace the capital words with appropriate values
// FILE: app/code/CODE_POOL/MODULE_NAMESPACE/MODULE_NAME/Model/Attribute/Source/Type/SELECT.php
/**
* Reference list for adding new form fields to an exiting
* entity's attribute.
*/
// Find/Replace:
// ModuleName
// EntityName
// TabName
class FirstScribe_ModuleName_Block_Adminhtml_EntityName_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
public function _prepareForm()
{
// ... AFTER $fieldset = $form->addFieldSet(/* ... params */);
// Select Dropdown
$fieldset->addField(
'attribute_code', // <!-- Change this
'select',
array(
'label' => Mage::helper('identifier')->__('Label'), // <!-- Change this and "identifier"
'name' => 'attribute_code', // <!-- Change this
'required' => false, // <!-- Change if desired
// @see https://gist.github.com/dfelton/8ca84c94ee0b57e346a3e7469d814214#file-300-select-php
// change to reflect your class name
'values' => Mage::getSingleton('identifier/attribute_source_type_select')->toOptionArray(),
)
);
// .. BEFORE $form->setValues($formValues);
}
}
<?php
// This file should be located here, replace the capital words with appropriate values
// FILE: app/code/CODE_POOL/MODULE_NAMESPACE/MODULE_NAME/Model/Attribute/Source/Type/SELECT.php
// Also consider renaming the class itself, instead of ending with "Select" use the attribute code or something.
// When doing so remember to rename the class below
// Modify the contents of getOptions(), rename class to accurate value.
/**
* Example attribute source for custom select attribute added to an entity.
*
* @category FirstScribe
* @package FirstScribe_ModuleName
*/
class FirstScribe_ModuleName_Model_Attribute_Source_Type_Select extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
public function getAllOptions()
{
if (is_null($this->_options)) {
// Modify the below logic to meet your extension's needs
$this->_options = array(
array(
'label' => Mage::helper('firstscribe_moduleName')->__('Option 1'),
'value' => 'option-1',
),
array(
'label' => Mage::helper('firstscribe_moduleName')->__('Option 2'),
'value' => 'option-2'
),
);
}
return $this->_options;
}
public function toOptionArray()
{
return $this->getAllOptions();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment