Forked from mikewhitby/Namespace_Module_Model_Entity.php
Created
December 28, 2015 09:31
-
-
Save sunel/ef95e9359a7e193c1531 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config> | |
<modules> | |
<Namespace_Module> | |
<version>0.1.0</version> | |
</Namespace_Module> | |
</modules> | |
<global> | |
<models> | |
<namespace_module> | |
<class>Namespace_Module_Model</class> | |
<resourceModel>namespace_module_resource</resourceModel> | |
</namespace_module> | |
<namespace_module_resource> | |
<class>Namespace_Module_Model_Resource</class> | |
<entities> | |
<entity> | |
<table>namespace_module_entity</table> | |
</guide> | |
</entities> | |
</namespace_module_resource> | |
</models> | |
<resources> | |
<namespace_module_setup> | |
<setup> | |
<module>Namespace_Module</module> | |
<class>Namespace_Module_Model_Entity_Setup</class> | |
</setup> | |
</namespace_module_setup> | |
</resources> | |
</global> | |
</config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Namespace_Module_Model_Entity extends Mage_Core_Model_Abstract | |
{ | |
/** | |
* Initialization | |
*/ | |
public function _construct() | |
{ | |
$this->_init('namespace_module/entity'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Namespace_Module_Model_Entity_Setup extends Mage_Eav_Model_Entity_Setup | |
{ | |
/** | |
* Default entities and attributes | |
* | |
* @return array | |
*/ | |
public function getDefaultEntities() | |
{ | |
return array( | |
'namespace_module_entity' => array( | |
'entity_model' => 'namespace_module/entity', | |
'table' => 'namespace_module/entity', | |
'attributes' => array( | |
'a_varchar' => array( | |
'type' => 'varchar', | |
'label' => 'A varchar', | |
'input' => 'text', | |
'sort_order' => 10, | |
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, | |
'group' => 'General Information', | |
), | |
'an_int' => array( | |
'type' => 'int', | |
'label' => 'An int', | |
'input' => 'select', | |
'sort_order' => 15, | |
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, | |
'group' => 'General Information', | |
), | |
'a_text' => array( | |
'type' => 'text', | |
'label' => 'A text', | |
'input' => 'textarea', | |
'sort_order' => 20, | |
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, | |
'group' => 'General Information', | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Namespace_Module_Model_Resource_Entity extends Mage_Eav_Model_Entity_Abstract | |
{ | |
/** | |
* Resource initialization | |
*/ | |
protected function _construct() | |
{ | |
$resource = Mage::getSingleton('core/resource'); | |
$this->setType('namespace_module_entity')->setConnection( | |
$resource->getConnection('namespace_module_read'), | |
$resource->getConnection('namespace_module_write') | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Namespace_Module_Model_Resource_Entity_Collection extends Mage_Eav_Model_Entity_Collection_Abstract | |
{ | |
/** | |
* Initialization | |
*/ | |
public function _construct() | |
{ | |
$this->_init('namespace_module/entity'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* @var $installer Namespace_Module_Model_Entity_Setup */ | |
$installer = $this; | |
$installer->startSetup(); | |
/** | |
* Create entity table | |
*/ | |
$table = $installer->getConnection() | |
->newTable($installer->getTable('namespace_module/entity')) | |
->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( | |
$installer->getIdxName('namespace_module/entity', array('entity_type_id')), | |
array('entity_type_id') | |
)->addIndex( | |
$installer->getIdxName('namespace_module/entity', array('attribute_set_id')), | |
array('attribute_set_id') | |
)->addForeignKey( | |
$installer->getFkName( | |
'namespace_module/entity', | |
'entity_type_id', | |
'eav/entity_type', | |
'entity_type_id' | |
), | |
'entity_type_id', | |
$installer->getTable('eav/entity_type'), | |
'entity_type_id', | |
Varien_Db_Ddl_Table::ACTION_CASCADE, | |
Varien_Db_Ddl_Table::ACTION_CASCADE | |
)->setComment('Namespace Entity Table'); | |
$installer->getConnection()->createTable($table); | |
/** | |
* Create value tables | |
*/ | |
$tableTypes = array( | |
'varchar' => Varien_Db_Ddl_Table::TYPE_INTEGER, | |
'int' => Varien_Db_Ddl_Table::TYPE_INTEGER, | |
'text' => Varien_Db_Ddl_Table::TYPE_TEXT, | |
'decimal' => Varien_Db_Ddl_Table::TYPE_DECIMAL, | |
'datetime' => Varien_Db_Ddl_Table::TYPE_DATETIME, | |
); | |
foreach ($tableTypes as $tableName => $dataType) { | |
$table = $installer->getConnection() | |
->newTable($installer->getTable(array('namespace_module/entity', $tableName)) | |
)->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', | |
$dataType, | |
null, | |
array(), | |
'Value' | |
)->addIndex( | |
$installer->getIdxName( | |
array('namespace_module/entity', $tableName), | |
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( | |
$installer->getIdxName( | |
array('namespace_module/entity', $tableName), | |
array('attribute_id') | |
), | |
array('attribute_id') | |
)->addIndex( | |
$installer->getIdxName( | |
array('namespace_module/entity', $tableName), | |
array('store_id') | |
), | |
array('store_id') | |
)->addIndex( | |
$installer->getIdxName( | |
array('namespace_module/entity', $tableName), | |
array('entity_id') | |
), | |
array('entity_id') | |
)->addForeignKey( | |
$installer->getFkName( | |
array('namespace_module/entity', $tableName), | |
'attribute_id', | |
'eav/attribute', | |
'attribute_id' | |
), | |
'attribute_id', | |
$installer->getTable('eav/attribute'), | |
'attribute_id', | |
Varien_Db_Ddl_Table::ACTION_CASCADE, | |
Varien_Db_Ddl_Table::ACTION_CASCADE | |
)->addForeignKey( | |
$installer->getFkName( | |
array('namespace_module/entity', $tableName), | |
'entity_id', | |
'namespace_module/entity', | |
'entity_id' | |
), | |
'entity_id', | |
$installer->getTable('namespace_module/entity'), | |
'entity_id', | |
Varien_Db_Ddl_Table::ACTION_CASCADE, | |
Varien_Db_Ddl_Table::ACTION_CASCADE | |
)->addForeignKey( | |
$installer->getFkName( | |
array('namespace_module/entity', $tableName), | |
'store_id', | |
'core/store', | |
'store_id' | |
), | |
'store_id', | |
$installer->getTable('core/store'), | |
'store_id', | |
Varien_Db_Ddl_Table::ACTION_CASCADE, | |
Varien_Db_Ddl_Table::ACTION_CASCADE | |
)->setComment("Namespace entity $tableName attribute backend table"); | |
$installer->getConnection()->createTable($table); | |
} | |
/** | |
* Install entity types, attributes, attribute sets and groups | |
*/ | |
$installer->installEntities(); | |
$installer->endSetup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment