Issue in displaying store multi-select drop down in custom module
-
12-12-2019 - |
Question
I have a custom community module in which I want to allow store multi-select drop-down.
I am already displaying the drop-down but when I save it's value and then, if I edit the form, I don't see the selected value highlighting though the value is stored correctly in database.
Can anybody let me know, what am I missing in the form ?
Solution
you can add this in to your Form.php
$fieldset->addField('store_id','multiselect',array(
'name' => 'stores[]',
'label' => Mage::helper('banners')->__('Store View'),
'title' => Mage::helper('banners')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true)
));
and you must have function in your module with _afterLoad
function
class Mage_Banners_Model_Mysql4_Banners extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
// Note that the banners_id refers to the key field in your database table.
$this->_init('banners/banners', 'banners_id');
}
protected function _afterLoad(Mage_Core_Model_Abstract $object)
{
$select = $this->_getReadAdapter()->select()
->from($this->getTable('banners_store'))
->where('banners_id = ?', $object->getId());
if ($data = $this->_getReadAdapter()->fetchAll($select)) {
$storesArray = array();
foreach ($data as $row) {
$storesArray[] = $row['store_id'];
}
$object->setData('store_id', $storesArray);
}
return parent::_afterLoad($object);
}
above code is just for your help you can set it as per your column name and database tables
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange