2

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

  }
  • where is you saveMethod add that too. – Qaisar Satti Mar 1 '16 at 6:50
  • talking about this 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))), – Qaisar Satti Mar 1 '16 at 7:00
  • Multi-select values are generally saved as comma separated values. – Prateek Mar 1 '16 at 7:12
  • @Qaisar Satti, updated the post, public function saveAction() {..) is there – Abdul Ghaffar Mar 1 '16 at 7:12
  • @Prateek , same I want to do – Abdul Ghaffar Mar 1 '16 at 7:13
4

add this before$model->setData($postData);

$postData['customer_group_id']=implode(',',$postData['customer_group_id']);
    $model->setData($postData);
  • it is saving just single value, but in debug mode on $postData['customer_group_id'] is (string) 3,4 but in db it just 3 – Abdul Ghaffar Mar 1 '16 at 7:53
  • check the db field it is varcher? maybe problem there.. or change the name of field something other then column name. – Qaisar Satti Mar 1 '16 at 7:55
  • that worked, how should I render it to grid, as it is not an attribute, facing some issues – Abdul Ghaffar Mar 1 '16 at 8:57
  • yes you have to rendered for the grid. – Qaisar Satti Mar 1 '16 at 9:04
6

Solution 1:

1) change customer_group_id to varchar instead of integer

2) add this before$model->setData($postData);

$postData['customer_group_id']=implode(',',$postData['customer_group_id']);
$model->setData($postData);

Solution 2:

1) remove customer_group_id from your current table

2) make new table to store customer_group_id and your primary key of current table and store that combinatation.

  • @NagarajuKasa "minesh patel" – Minesh Patel Oct 16 '17 at 12:04
  • hi @minesh patel superb yar you have helped me lot thank you very much keep rocking...have a nice day – Nagaraju Kasa Oct 16 '17 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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