Image Magento-新建模块编辑操作正在创建新项
我正在创建一个模块来添加横幅。现在,当我想要编辑横幅时,我遇到了一个问题 问题是,当我单击“保存”时,Magento会创建一个新的横幅,而不会修改原始横幅。它发生在我修改某些内容时,如果我不修改任何内容并单击“保存” 我还有一个问题。我有一个图像文件,它工作得很好,但当我点击“编辑”时,它会显示预览图像和删除复选框,但字段是空的,如果我保存横幅,它会将图像字段保留为空Image Magento-新建模块编辑操作正在创建新项,image,magento,edit,Image,Magento,Edit,我正在创建一个模块来添加横幅。现在,当我想要编辑横幅时,我遇到了一个问题 问题是,当我单击“保存”时,Magento会创建一个新的横幅,而不会修改原始横幅。它发生在我修改某些内容时,如果我不修改任何内容并单击“保存” 我还有一个问题。我有一个图像文件,它工作得很好,但当我点击“编辑”时,它会显示预览图像和删除复选框,但字段是空的,如果我保存横幅,它会将图像字段保留为空 我希望你能帮助我。提前感谢,如果您需要更多信息,请向我咨询。关于保存横幅,请根据您的情况调整以下代码: 这里重要的是在保存时使用
我希望你能帮助我。提前感谢,如果您需要更多信息,请向我咨询。关于保存横幅,请根据您的情况调整以下代码: 这里重要的是在保存时使用注册表。 保存时,还要检查$data中是否提供了标题id。您的form.php必须提供它,请在$form->setValues(…)行之前添加以下代码: 当然,您必须在保存之前验证用户的输入。在横幅模型中使用受保护的方法_beforeSave(),或在保存操作中直接在控制器中实现这些输入验证
/**
* Common init to almost all actions
*/
protected function _initAction(){
$this->_title ($this->__("Banner"));
$this->loadLayout();
$this->_setActiveMenu('mymenu/banner');
$this->_addBreadcrumb(Mage::helper('banner')->__('Banners'), Mage::helper('banner')->__('Items'));
}
if(! Mage::registry('current_banner')){
Mage::register('current_banner', Mage::getModel('banner/item'));
}
$id = $this->getRequest()->getParam('id');
if (!is_null($id)) {
$model = Mage::registry('current_banner')->load($id);
if (! $model->getId()) {
$this->_getSession()->addError(Mage::helper('banner')->__('This banner item no longer exists'));
$this->_redirect('*/*/');
return;
}
}
return $this;
}
/**
* Banner edit page
*/
public function editAction(){
$this->_initAction();
$this->_title('Banner Edit');
// 1. Get ID and create model
$banner = Mage::registry('current_banner');
// 2. set entered data if there had errors when we do save
$data = $this->_getSession()->getBannerData(true);
// 3. restore data from SESSION and provide a correct date format
if (!empty($data)) {
$banner->addData($data);
}
// 4. Build Edit form
$this->_addBreadcrumb(Mage::helper('banner')->__('Edit banner Item'), Mage::helper('banner')->__('Edit Banner Item'));
$this->_addContent($this->getLayout()->createBlock('banner/adminhtml_banner'));
$this->renderLayout();
}
/**
* Subscritpion save process
*/
public function saveAction(){
$this->_initAction();
$banner = Mage::registry('current_banner');
$data = $this->getRequest()->getParams();
if ($data) {
try {
$banner->addData($data);
$banner->save();
$this->_getSession()->addSuccess(Mage::helper('banner')->__('The banner item has been saved.'));
if ($this->getRequest()->getParam('back', false)) {
$this->_redirect('*/*/edit', array('id' => $banner->getId(), '_current'=>true));
return;
}
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
$this->_getSession()->setBannerData($banner->getData());
$this->_redirectUrl($this->getUrl('*/*/edit', array('id' => $banner->getId())));
return;
}
}
$this->_redirectUrl($this->getUrl('*/adminhtml_overview'));
}
关于保存横幅,请根据您的情况调整以下代码: 这里重要的是在保存时使用注册表。 保存时,还要检查$data中是否提供了标题id。您的form.php必须提供它,请在$form->setValues(…)行之前添加以下代码: 当然,您必须在保存之前验证用户的输入。在横幅模型中使用受保护的方法_beforeSave(),或在保存操作中直接在控制器中实现这些输入验证
/**
* Common init to almost all actions
*/
protected function _initAction(){
$this->_title ($this->__("Banner"));
$this->loadLayout();
$this->_setActiveMenu('mymenu/banner');
$this->_addBreadcrumb(Mage::helper('banner')->__('Banners'), Mage::helper('banner')->__('Items'));
}
if(! Mage::registry('current_banner')){
Mage::register('current_banner', Mage::getModel('banner/item'));
}
$id = $this->getRequest()->getParam('id');
if (!is_null($id)) {
$model = Mage::registry('current_banner')->load($id);
if (! $model->getId()) {
$this->_getSession()->addError(Mage::helper('banner')->__('This banner item no longer exists'));
$this->_redirect('*/*/');
return;
}
}
return $this;
}
/**
* Banner edit page
*/
public function editAction(){
$this->_initAction();
$this->_title('Banner Edit');
// 1. Get ID and create model
$banner = Mage::registry('current_banner');
// 2. set entered data if there had errors when we do save
$data = $this->_getSession()->getBannerData(true);
// 3. restore data from SESSION and provide a correct date format
if (!empty($data)) {
$banner->addData($data);
}
// 4. Build Edit form
$this->_addBreadcrumb(Mage::helper('banner')->__('Edit banner Item'), Mage::helper('banner')->__('Edit Banner Item'));
$this->_addContent($this->getLayout()->createBlock('banner/adminhtml_banner'));
$this->renderLayout();
}
/**
* Subscritpion save process
*/
public function saveAction(){
$this->_initAction();
$banner = Mage::registry('current_banner');
$data = $this->getRequest()->getParams();
if ($data) {
try {
$banner->addData($data);
$banner->save();
$this->_getSession()->addSuccess(Mage::helper('banner')->__('The banner item has been saved.'));
if ($this->getRequest()->getParam('back', false)) {
$this->_redirect('*/*/edit', array('id' => $banner->getId(), '_current'=>true));
return;
}
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
$this->_getSession()->setBannerData($banner->getData());
$this->_redirectUrl($this->getUrl('*/*/edit', array('id' => $banner->getId())));
return;
}
}
$this->_redirectUrl($this->getUrl('*/adminhtml_overview'));
}
在你的问题中添加有问题的代码将非常有帮助。我在另一篇文章中附加了一些文件(网格、表单和控制器,它们都很短^^^):感谢你的评论(我总是需要编辑这个,我讨厌当我点击“回车”时它会发送评论。)在你的问题中添加有问题的代码将非常有帮助。我在另一篇文章中附加了一些文件(网格、表单和控制器,它们都很短^^^):感谢你的评论(我总是需要编辑这个,我讨厌当我点击“回车”时它会发送注释jaja。)我已经尝试使用Mage::registry,但没有工作。我不知道那是什么;)如何验证数据?我想在没有Magento的情况下使用php编程…检查我写的关于您的文件form.php的内容,您的横幅的实体id作为隐藏值丢失是的,我已经添加了,但也不起作用。我已经看到,当我进入编辑页面时,我拥有所有正确的数据和id。非常感谢您与我一起使用的时间。好的,现在它工作了。正如你所说,我已经使用了一个隐藏字段,并且在控制器中进行了一些更改,现在正在工作。非常感谢。我已经尝试使用Mage::registry,但不起作用。我不知道那是什么;)如何验证数据?我想在没有Magento的情况下使用php编程…检查我写的关于您的文件form.php的内容,您的横幅的实体id作为隐藏值丢失是的,我已经添加了,但也不起作用。我已经看到,当我进入编辑页面时,我拥有所有正确的数据和id。非常感谢您与我一起使用的时间。好的,现在它工作了。正如你所说,我已经使用了一个隐藏字段,并且在控制器中进行了一些更改,现在正在工作。多谢各位。