在magento中创建编辑表单时出错

时间:2014-04-26 11:44:27

标签: magento

Fatal error: Call to a member function setData() on a non-object in C:\xampp\htdocs\magento3\app\code\core\Mage\Adminhtml\Block\Widget\Form\Container.php on line 129

Edit.php

    <?php
class Magecom_Magecomslider_Block_Adminhtml_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_controller = 'adminhtml_magecomslider';
        $this->_mode = 'edit';

        $this->_updateButton('save', 'label', Mage::helper('magecom_magecomslider')->__('Save Image'));
        $this->_updateButton('save', 'id', 'save_button');
        $this->_updateButton('delete', 'label', Mage::helper('magecom_magecomslider')->__('Delete Image'));

        if( $this->getRequest()->getParam($this->_objectId) ) {
            $magecomSlider = Mage::getModel('magecom_magecomslider/magecomslider')
                ->load($this->getRequest()->getParam($this->_objectId));
            Mage::register('magecomSlider', $magecomSlider);
        }
    }

    public function getHeaderText()
    {
        return Mage::helper('magecom_magecomslider')->__('Add Image');
    }
}

form.php的

    <?
class Mage_Adminhtml_Block_Magecom_Magecomslider_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {exit("asd");exit("asd");
        $magecomSlider = Mage::registry('magecomSlider');
        //$product = Mage::getModel('catalog/product')->load($review->getEntityPkValue());
        //$customer = Mage::getModel('customer/customer')->load($review->getCustomerId());
        $magecomslider = Mage::getModel('magecom_magecomslider/magecomslider')->load($this->getRequest()->getParam('id'));

        $form = new Varien_Data_Form(array(
            'id'        => 'edit_form',
            'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'), 'ret' => Mage::registry('ret'))),
            'method'    => 'post'
        ));

        $fieldset = $form->addFieldset('magecomslider', array('legend' => Mage::helper('magecom_magecomslider')->__('Add Image'), 'class' => 'fieldset-wide'));

        $fieldset->addField('magecomslider', 'image', array(
            'label'     => Mage::helper('magecom_magecomslider')->__('Image'),
            'text'      => 'magecomslider',
        ));

        $form->setUseContainer(true);
        $form->setValues($review->getData());
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

MagecomsliderController.php

<?php
class Magecom_Magecomslider_Adminhtml_MagecomsliderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Magecom Slider'));

        $this->loadLayout();
        $this->_setActiveMenu('magecom_magecomslider');
        $this->_addBreadcrumb(Mage::helper('magecom_magecomslider')->__('Magecom Slider'), Mage::helper('magecom_magecomslider')->__('Magecom Slider'));
        $this->renderLayout();

    }
    public function massAddAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select products.'));
        } else {
            if (!empty($productIds)) {
                try {
                    $countItems = count($productIds);
                    if($countItems>2 || $countItems<2)
                    {
                        $this->_getSession()->addError($this->__('You should select only 2 products.'));
                    }else{
                        $slider_items = Mage::getModel('magecom_magecomslider/magecomslider');
                        $collection_slider_items = $slider_items->getCollection();
                        $collection_slider_items_count = $collection_slider_items->count();
                        if($collection_slider_items_count==2)
                        {
                            $this->_getSession()->addError($this->__('Sliders exists.'));
                        }else{
                            foreach ($productIds as $productId) {
                                $data = array('product_id' => $productId);
                                $model = Mage::getModel('magecom_magecomslider/magecomslider');
                                $model->setData($data);
                                $model->save();
                            }
                            $this->_getSession()->addSuccess(
                                $this->__('Products added to the slideshow.')
                            );
                        }    
                    }
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/index');
    }
    public function gridAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    public function deleteAction()
    {
        try {
            $slider_items = Mage::getModel('magecom_magecomslider/magecomslider');
            $collection_slider_items = $slider_items->getCollection();
            foreach($collection_slider_items as $item)
            {
                $item->delete();
            }
            $this->_getSession()->addSuccess(
                $this->__('Sliders deleted successfully.')
            );  
        }catch (Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        }
        $this->_redirect('*/*/index');
    }
    public function editAction()
    {
        $this->loadLayout();
        $product_id = $this->getRequest()->getParam('id');

        $slider_items = Mage::getModel('magecom_magecomslider/magecomslider');
        $collection_slider_items = $slider_items->getCollection();
        $collection_slider_items_count = $collection_slider_items->count();
        if($collection_slider_items_count==2)
        {

        }else{
            $this->_getSession()->addError($this->__('Please select products for slider.'));
        }
        $this->renderLayout();
        //exit($product_id);
    }
    public function saveAction()
    {
        exit("Save");
    }
}

请在Form.php退出中查看不起作用,我认为它的意思是这个形式没有被称为,我不知道为什么。

magecomslider.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <adminhtml_magecomslider_index>
        <reference name="content">
            <block type="magecom_magecomslider/adminhtml_magecomslider" name="magecomslider.grid_container" />
        </reference>
    </adminhtml_magecomslider_index>

    <adminhtml_magecomslider_new>
        <update handle="adminhtml_magecomslider_edit"/>
    </adminhtml_magecomslider_new>

    <adminhtml_magecomslider_edit>
        <reference name="content">
            <block type="magecom_magecomslider/adminhtml_edit" name="magecomslider.edit" />
        </reference>
    </adminhtml_magecomslider_edit>
    <adminhtml_magecomslider_grid>
        <block type="core/text_list" name="root" output="toHtml">
            <block type="magecom_magecomslider/adminhtml_magecomslider" name="magecomslider.grid_container" />
        </block>
    </adminhtml_magecomslider_grid>
    <adminhtml_magecomslider_edit>
        <reference name="content">
            <block type="magecom_magecomslider/adminhtml_edit" name="magecomslider.form_container" />
        </reference>
    </adminhtml_magecomslider_edit>
</layout>

Magecomslider.php - 模型

<?php
class Magecom_Magecomslider_Model_Magecomslider extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('magecom_magecomslider/magecomslider');
    }
}

Magecomslider.php - 资源模型

<?php
class Magecom_Magecomslider_Model_Resource_Magecomslider extends Mage_Core_Model_Mysql4_Abstract
{
    protected function _construct()
    {
        $this->_init('magecom_magecomslider/magecomslider', 'id');
    }
}

Collection.php

<?php
class Magecom_Magecomslider_Model_Resource_Magecomslider_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    protected function _construct()
    {
        $this->_init('magecom_magecomslider/magecomslider');
    }
}

2 个答案:

答案 0 :(得分:3)

我在edit.php档案

中遇到了同样的错误

现在位于 _construct()功能

添加以下行:

$this->_blockGroup = 'your block group here';

希望这会有用

感谢

答案 1 :(得分:1)

在此处仔细检查您的阻止名称: edit.php

$ this-&gt; _blockGroup ='your_block_group / group_name';

如果您没有此行,请将其添加到您的edit.php

享受编码....

感谢