I want custom increment_id field in custom module. Like sales_flat_order increment_id. Is EAV table format is best solution?
2 Answers
- Add installer in your custom module to create new entity
$installer->addEntityType('qquoteadv', array( 'entity_model' => 'qquoteadv/qqadvcustomer', 'table' => 'quoteadv/customer', 'increment_model' => 'eav/entity_increment_numeric', 'increment_per_store' => true ));
where quoteadv/customer - custom table will contain increment_id field.
- Add Numeric.php in your custom module.
public function getNextId() { $storeId = Mage::app()->getStore()->getId(); $nextNum = Mage::getSingleton('eav/config')->getEntityType($aEntityTypes) ->fetchNewIncrementId($storeId); $rowPrefix = Mage::getStoreConfig(self::PARAM_PREFIX); return $this->format($nextNum); } else { return parent::getNextId(); } }
if you want to do this in flat tables not in EAV like structure you can do by just following step
Your table needs to have store_id and increment_id field
In config.xml
files resources
node add this
<alias_setup>
<setup>
<module>Package_Module</module>
<class>Package_Module_Model_Resource_Setup</class> <!--add this class-->
</setup>
<connection>
<use>core_setup</use>
</connection>
</alias_setup>
create Package_Module_Model_Resource_Setup
class in respective location
<?php
class Package_Module_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{
}
In your installer/Upgrade script add following code
$installer->addEntityType('yourentityname', array(
'entity_model' => 'model_alias/model', //entity model same as table in flat table case
'table' => 'model_alias/model',
'increment_model' => 'eav/entity_increment_numeric',
'increment_per_store' => true
));
in resource model class add following method
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
if (!$object->getIncrementId()) {
/* @var $entityType Mage_Eav_Model_Entity_Type */
$entityType = Mage::getModel('eav/entity_type')->loadByCode('yourentityname');
$object->setIncrementId($entityType->fetchNewIncrementId($object->getStoreId()));
}
parent::_beforeSave($object);
return $this;
}