I have created a custom grid, I need to add a multi select field. Multi select field will be getting value from "customer_group" table as you can see in
/www/Abc/app/code/local/Abc/Bar/Block/Adminhtml/Baz/Edit/Form.php,
I am unable to save my multi selected values to to db(abc_bar/baz) in field "customer_group_id" and render them in grid,
my db looks like this
/app/code/local/Abc/Bar/sql/abc_bar_setup/mysql4-install-1.0.0.php
<?php
$installer = $this;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('abc_bar/baz'))
->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'ID')
->addColumn('name', Varien_Db_Ddl_Table::TYPE_CLOB, 0, array(
'nullable' => false,
), 'Name')
->addColumn($installer->getTable('abc_bar_baz'), 'emails', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'comment' => 'Sales Emails for Order type'
)
->addColumn($installer->getTable('abc_bar_baz'), 'ordertype_description', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'comment' => 'Order type description'
)
->addColumn($installer->getTable('abc_bar_baz'), 'customer_group_id', array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
'length' => 11,
'nullable' => false,
'comment' => 'customer group to bind order type'
)
;
$installer->getConnection()->createTable($table);
$installer->endSetup();
and my grid is
www/Abc/app/code/local/abc/Bar/Block/Adminhtml/Baz/Grid.php
<?php
class Abc_Bar_Block_Adminhtml_Baz_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setDefaultSort('id');
$this->setId('abc_bar_baz_grid');
$this->setDefaultDir('asc');
$this->setSaveParametersInSession(true);
}
protected function _getCollectionClass() {
return 'abc_bar/baz_collection';
}
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('id', array(
'header' => $this->__('ID'),
'align' => 'right',
'width' => '50px',
'index' => 'id'
)
);
$this->addColumn('name', array(
'header' => $this->__('Name'),
'index' => 'name'
)
);
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}
And my form is
/www/Abc/app/code/local/Abc/Bar/Block/Adminhtml/Baz/Edit/Form.php
class Abc_Bar_Block_Adminhtml_Baz_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {
public function __construct() {
parent::__construct();
$this->setId('abc_bar_baz_form');
$this->setTitle($this->__('Baz Information'));
}
protected function _prepareForm() {
$model = Mage::registry('abc_bar');
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post'
));
$fieldset = $form->addFieldset('base_fieldset', array(
'legend' => Mage::helper('checkout')->__('Baz Information'),
'class' => 'fieldset-wide',
));
if ($model->getId()) {
$fieldset->addField('id', 'hidden', array(
'name' => 'id',
));
}
$fieldset->addField('name', 'text', array(
'name' => 'name',
'label' => Mage::helper('checkout')->__('Name'),
'title' => Mage::helper('checkout')->__('Name'),
'required' => true,
));
$fieldset->addField('emails', 'text', array(
'name' => 'emails',
'label' => Mage::helper('checkout')->__('Emails'),
'title' => Mage::helper('checkout')->__('Emails'),
'required' => FALSE,
));
$fieldset->addField('ordertype_description', 'text', array(
'name' => 'ordertype_description',
'label' => Mage::helper('checkout')->__('Order type description'),
'title' => Mage::helper('checkout')->__('Order type description'),
'required' => FALSE,
));
//Load all groups and get their code and id
foreach (Mage::getModel('customer/group')->getCollection() as $group) {
$select[] = array('value' => $group->getCustomerGroupId(), 'label' => $group->getCustomerGroupCode());
}
//Add new field to the form
$fieldset->addField('customer_group_id', 'multiselect', array(
'label' => Mage::helper('checkout')->__('Select Customer Groupp'),
'title' => Mage::helper('checkout')->__('Select Customer Group'),
'name' => 'customer_group_id',
'required' => false,
'values' => $select
));
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
And my actions i.e save and other are here
/www/Abc/app/code/local/Abc/Bar/controllers/Adminhtml/BazController.php
class Abc_Bar_Adminhtml_BazController extends Mage_Adminhtml_Controller_Action {
public function indexAction() {
$this->_initAction()
->renderLayout();
}
public function newAction() {
$this->_forward('edit');
}
public function editAction() {
$this->_initAction();
$id = $this->getRequest()->getParam('id');
$model = Mage::getModel('abc_bar/baz');
if ($id) {
$model->load($id);
if (!$model->getId()) {
Mage::getSingleton('adminhtml/session')->addError($this->__('This baz no longer exists.'));
$this->_redirect('*/*/');
return;
}
}
$this->_title($model->getId() ? $model->getName() : $this->__('New Baz'));
$data = Mage::getSingleton('adminhtml/session')->getBazData(true);
if (!empty($data)) {
$model->setData($data);
}
Mage::register('abc_bar', $model);
$this->_initAction()
->_addBreadcrumb($id ? $this->__('Edit Baz') : $this->__('New Baz'), $id ? $this->__('Edit Baz') : $this->__('New Baz'))
->_addContent($this->getLayout()->createBlock('abc_bar/adminhtml_baz_edit')->setData('action', $this->getUrl('*/*/save')))
->renderLayout();
}
public function saveAction() {
if ($postData = $this->getRequest()->getPost()) {
$model = Mage::getSingleton('abc_bar/baz');
$model->setData($postData);
try {
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The baz has been saved.'));
$this->_redirect('*/*/');
return;
} catch (Mage_Core_Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this baz.'));
}
Mage::getSingleton('adminhtml/session')->setBazData($postData);
$this->_redirectReferer();
}
}
public function messageAction() {
$data = Mage::getModel('abc_bar/baz')->load($this->getRequest()->getParam('id'));
echo $data->getContent();
}
protected function _initAction() {
$this->loadLayout()
// Make the active menu match the menu config nodes (without 'children' inbetween)
->_setActiveMenu('sales/abc_bar_baz')
->_title($this->__('Sales'))->_title($this->__('Baz'))
->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
->_addBreadcrumb($this->__('Baz'), $this->__('Baz'));
return $this;
}
protected function _isAllowed() {
return Mage::getSingleton('admin/session')->isAllowed('sales/abc_bar_baz');
}
}
Best Answer
add this before
$model->setData($postData);