Magento - 新模块编辑操作正在创建一个新项目

发布于 2024-12-10 20:00:27 字数 247 浏览 0 评论 0原文

我正在创建一个模块来添加横幅。现在当我想编辑横幅时遇到问题。

问题是,当我单击“保存”时,Magento 会创建一个新横幅,并且不会修改原始横幅。当我修改某些内容并且不修改任何内容并单击“保存”时,就会发生这种情况。

我还有另一个问题。我有一个图像字段,它工作得很好,但是当我单击“编辑”时,它显示预览图像和删除复选框,但该字段为空,如果我保存横幅,它会使图像字段为空。

我希望你能帮助我。预先感谢,如果您需要更多信息,请向我索取。

I am creating a module to add banners. Now I have a problem when I want to edit a banner.

The problem is that when I click "save", Magento creates a new banner and doesn't modify the original banner. It happens when I modify something and if I don't modify anything and click "save".

I have another problem. I have an image fiel and it's working great but when I click "edit", it shows the preview image and the delete check box but the field is empty and if I save the banner it leaves the image field empty.

I hope you can help me. Thanks in advance, if you need more information ask me for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-12-17 20:00:27

关于保存横幅,请根据您的情况调整以下代码:
这里重要的是保存时使用注册表。
保存时还要检查 $data 中是否提供了横幅 ID。您的 form.php 必须提供它,在 $form->setValues(...) 行之前添加以下代码:

$model = Mage::registry('BannerManagement_data');
if ($model->getEntityId()) {
   $fieldset->addField('entity_id', 'hidden', array('name' => 'entity_id'));// or banner_id depends on what id title you gave in your database table
}

当然,您必须在保存之前验证用户的输入。在横幅模型中使用受保护的方法 _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'));
}

about the saving of your banner, adapt the following code to your situation:
What is important here is to use the registry when saving.
Check too that the banner id is provided in $data when saving. Your form.php must provide it, add the code below before $form->setValues(...) line:

$model = Mage::registry('BannerManagement_data');
if ($model->getEntityId()) {
   $fieldset->addField('entity_id', 'hidden', array('name' => 'entity_id'));// or banner_id depends on what id title you gave in your database table
}

Of course you have to validate the input of the users beforeSave. Use the protected method _beforeSave() in your banner model to implement these input validation or in the controller directly in the save action.

/**
 * 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'));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文