我为管理员创建了magento 1.8自定义模块。 它有网格和添加项目选项。在数据库表中,它有2个字段ID [自动增量]和NAME。 一切正常,但是当我点击“保存”按钮时,它会显示成功消息,但名称字段数据不会保存到数据库中。只有ID递增并显示在网格中
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();
}
我的 saveAction 功能
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('*/*/');
}
答案 0 :(得分:4)
您的代码是正确的。如果您有字段名称" 名称"在数据库表中然后使用此代码
$fieldset->addField('Name', 'text', array(
'label' => Mage::helper('fondation')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'Name',
));