0

I want custom increment_id field in custom module. Like sales_flat_order increment_id. Is EAV table format is best solution?

2
  • You can create in normal table as well Mar 31, 2016 at 11:51
  • set your increment_id in before save method if object don't have id on it Mar 31, 2016 at 11:52

2 Answers 2

1
  1. 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.

  1. 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();
            }
        }

0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.