Monday, October 22, 2012

Alter a form in Admin

Here I am adding a storied in a form

Step 1: Alter table in database and add a field storeid in Table

Step 2: You can alter it from install file

<?php
      
      $installer = $this;
      
      $installer->startSetup();
      
      $installer->run("
      
DROP TABLE IF EXISTS {$this->getTable('locator')};
CREATE TABLE {$this->getTable('locator')} (
  `locator_id` int(11) unsigned NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  `address` varchar(255) NOT NULL default '',
  `city` varchar(100) NOT NULL default '',
  `state` varchar(2) NOT NULL default '',
  `postal_code` varchar(8) NOT NULL default '',
  `phone` varchar(20) default NULL,
  `longitude` double NOT NULL,
  `latitude` double NOT NULL,
  `status` smallint(6) NOT NULL default '0',
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  `storeid` smallint(6) NOT NULL default '0',
  PRIMARY KEY (`locator_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
      
          ");
      
      $installer->endSetup();

3) Create an array of dropdown of store

 public function toOptionArray()
    {
        $allStores = Mage::app()->getStores();
        $dataPoints= array();
        foreach ($allStores as $_eachStoreId => $val)
        {
            $_storeCode = Mage::app()->getStore($_eachStoreId)->getCode();
            $_storeName = Mage::app()->getStore($_eachStoreId)->getName();
            $_storeId = Mage::app()->getStore($_eachStoreId)->getId();

            //$dataPoints = array(
            //"value"=>$_storeId,
            //"label"=>$_storeName);
        $dataPoints[] =  array("value"=>$_storeId,"label"=>$_storeName);

          

        }
     

        return $dataPoints;
    
    }
Step 4: Show field in Form
$fieldset->addField('storeid', 'select', array(
        'name'  => 'storeid',
        'label'     => 'Store',
        'values'    => $this->toOptionArray(),
    ));

Step 5: Now Alter save function

 $locatorModel->setId($this->getRequest()->getParam('id'))
                        ->setName(trim($postData['name']))
                        ->setStatus($postData['status'])
                        ->setAddress(trim($postData['address']))
                        ->setCity(trim($postData['city']))
                        ->setState(trim($postData['state']))
                        ->setPostalCode(trim($postData['postal_code']))
                        ->setPhone(trim($postData['phone']))
                        ->setLongitude($longitude)
                        ->setLatitude($latitude)
                        ->setStoreid($storeid)
                        ->save();



No comments:

Post a Comment