0

I am developing custom admin module which save some information to custom flat table.

I have done these module before but today I am not able to save all the records from form to database table my "saveAction" not working properly.

I am not getting any error, and unable to find what is wrong with this. Can any one check and help me, where my code is wrong.

Mbm/Blocks/Block/Adminhtml/Blocksfrench/Edit/Tab/Form.php code

protected function _prepareForm()
    {
        $blocksfrench = Mage::registry('blocksfrench_data');
        $form = new Varien_Data_Form();

        $fieldset = $form->addFieldset('blocks_form', array('legend'=>Mage::helper('blocks')->__('Settings')));
        //$websiteId = Mage::getModel('core/store_group')->load($blocksfrench->getData('store_id'),'group_id');

        //$websitesThatDontHaveDonations = array(7,2,1);
        $fieldset->addField('id', 'hidden', array(
          'name'      => 'id',
          ));

        $fieldset->addField('title', 'text', array(
          'label'     => Mage::helper('blocks')->__('Title'),
          'name'      => 'title',
          'class'     => 'required-entry',
          'required'  => true,
          ));

        $fieldset->addField('link_url', 'text', array(
          'label'     => Mage::helper('blocks')->__('Link'),
          'required'  => true,
          'name'      => 'link_url',
          'class'     => 'required-entry validate-clean-url',
          ));


        $fieldset->addField('link_type', 'select', array(
          'label'     => Mage::helper('blocks')->__('Link Type'),
          'required'  => true,
          'name'      => 'link_type',
          'class'     => 'required-entry',
          'values'     => array(
                            array('value'=>'Internal','label'=>'Internal'),
                            array('value'=>'External','label'=>'External'),
                          ),
          ));

        $fieldset->addField('order', 'text', array(
          'label'     => Mage::helper('blocks')->__('Sort Order'),
          'name'      => 'order',
          'class'     => 'validate-digits',
          ));
        $fieldset->addField('store_id', 'hidden', array(
              'name'      => 'store_id',
        ));
        $fieldset->addField('store_view_id', 'hidden', array(
              'name'      => 'store_view_id',
        ));
        $fieldset->addField('website_id', 'hidden', array(
              'name'      => 'website_id',
        ));


        $this->setForm($form);
        $form->setValues($blocksfrench->getData());
        if (Mage::getSingleton('adminhtml/session')->getblocksData()) {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getblocksData());
            Mage::getSingleton('adminhtml/session')->setblocksData(null);
        } elseif (Mage::registry('blocks_data')) {
            $form->setValues(Mage::registry('blocks_data')->getData());
        }
        return parent::_prepareForm();
    }

Mbm/Blocks/Adminhtml/BlocksfrenchController.php saveAction code

public function saveAction()
    {
        $id = (int) $this->getRequest()->getParam('id');
        if ($data = $this->getRequest()->getPost()) {
            $storeId = 1;
            $storeViewId = 1;
            $websiteId = 1;
            $modelSave = Mage::getModel('blocks/blocks');
            if (isset($id)) {
                $modelSave->setData($data)->setId($data['id']);
            } else {
                $modelSave->setData($data);
            }
            $modelSave->setStoreId($storeId);
            $modelSave->setStoreViewId($storeViewId);
            $modelSave->setWebsiteId($websiteId);

            try {
                $modelSave->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customreport')->__('Store data was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $modelSave->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $modelSave->getId()));
                return;
            }
            }
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('customreport')->__('Unable to find item to save'));
            $this->_redirect('*/*/');
        }

(blocks/blocks)Collection print result

Mbm_Blocks_Model_Blocks Object
(
    [_eventPrefix:protected] => core_abstract
    [_eventObject:protected] => object
    [_resourceName:protected] => blocks/blocks
    [_resource:protected] => 
    [_resourceCollectionName:protected] => blocks/blocks_collection
    [_cacheTag:protected] => 
    [_dataSaveAllowed:protected] => 1
    [_isObjectNew:protected] => 
    [_data:protected] => Array
        (
            [form_key] => apyQdrEzlH6acqb8
            [id] => 
            [title] => L'entreprise Mano
            [link_url] => www.boutikm.com
            [link_type] => Internal
            [order] => 0
            [store_id] => 1
            [store_view_id] => 1
            [website_id] => 1
        )

    [_hasDataChanges:protected] => 1
    [_origData:protected] => 
    [_idFieldName:protected] => id
    [_isDeleted:protected] => 
    [_oldFieldsMap:protected] => Array
        (
        )

    [_syncFieldsMap:protected] => Array
        (
        )

)
1

1 Answer 1

0

So basically here is what's missing in my opinion:

In your controller, under the editAction() function you should have a piece of code that looks like this:

$data = Mage::getSingleton('adminhtml/session')->getFormData(true);

if (!empty($data)) {
   $model->setData($data);
}
Mage::register('mymodel_data', $model);

Then in your Form block, in the prepareForm function() you retrieve the data using a piece of code that looks like this:

if (Mage::getSingleton('adminhtml/session')->getMymodelData()) {
    form->setValues(Mage::getSingleton('adminhtml/session')->getMymodelData());
    Mage::getSingleton('adminhtml/session')->setMymodelData(null);
} elseif (Mage::registry('mymodel_data')) {
    $form->setValues(Mage::registry('mymodel_data')->getData());
}  

In your case, you should replace 'mymodel' with 'blocks'.

Also on a side note, I'm not sure if it's related to your problem but the following line:

$this->setForm($form);

Shouldn't be at the end of the prepareForm() function but right after this line:

$form = new Varien_Data_Form();

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.