문제

All I have created magento 1.8 custom module for admin. It has grid and the add item option. In database table it has 2 fields ID [Auto increment] and NAME. Everything working fine but when I click on save button it shows the success message but the name field data not saving to the database. only ID is incrementing and shown in the grid

protected function _prepareForm()
{
    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('fondation_form', array('legend'=>Mage::helper('fondation')->__('Item information')));
    $fieldset->addField('Name', 'text', array(
      'label'     => Mage::helper('fondation')->__('Name'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
    ));
    if (Mage::getSingleton('adminhtml/session')->getFondationData()) {
        $form->setValues(Mage::getSingleton('adminhtml/session')->getFondationData());
        Mage::getSingleton('adminhtml/session')->setFondationData(null);
    } elseif (Mage::registry('fondation_data')) {
        $form->setValues(Mage::registry('fondation_data')->getData());
    }
    return parent::_prepareForm();
}

my saveAction function

public function saveAction()
{
    if ($data = $this->getRequest()->getPost()) {
        $model = Mage::getModel('fondation/fondation');
        $model->setData($data)->setId($this->getRequest()->getParam('id'));
        try {
            $model->save();
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('fondation')->__('Item was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setFormData(false);

            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->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' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('fondation')->__('Unable to find item to save'));
    $this->_redirect('*/*/');
}
도움이 되었습니까?

해결책

Your code is correct. if you have the field name "Name" in database table then use this code

 $fieldset->addField('Name', 'text', array(
  'label'     => Mage::helper('fondation')->__('Name'),
  'class'     => 'required-entry',
  'required'  => true,
  'name'      => 'Name',
));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top