Php 我没有看到我的表单上的保存按钮,即使我已经在管理区Magento中设置了它
我是mangeto框架的新手,我正在学习在管理区创建表单。然而,几个小时以来,我一直无法找出我收到的错误: 可恢复错误:传递给Mage_Adminhtml_Controller_操作的参数1::_addContent()必须是Mage_Core_Block_抽象的实例,布尔给定,在第12行的/vagrant/magento/app/code/local/MasteringMagento/Example/controllers/Adminhtml/EventController.php中调用 下面是我的Edit.php文件和Form.php文件 Edit.php:Php 我没有看到我的表单上的保存按钮,即使我已经在管理区Magento中设置了它,php,forms,magento,Php,Forms,Magento,我是mangeto框架的新手,我正在学习在管理区创建表单。然而,几个小时以来,我一直无法找出我收到的错误: 可恢复错误:传递给Mage_Adminhtml_Controller_操作的参数1::_addContent()必须是Mage_Core_Block_抽象的实例,布尔给定,在第12行的/vagrant/magento/app/code/local/MasteringMagento/Example/controllers/Adminhtml/EventController.php中调用 下面
class MasteringMagento_Example_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action{
public function indexAction(){
$this->loadLayout();
$this->_addContent(
$this->getLayout()->createBlock('example/adminhtml_event_edit'));
//go straight to the php file to render the form. otherwise this will not perfomed.
$this->renderLayout();
}
public function saveAction(){
$eventID = $this->getRequest()->getParam('event_id');
$eventModel = Mage::getModel('example/event')->load($eventID);
if($data = $this->getRequest()->getPost()){
try{
$eventModel->addData($data)->save();
Mage::getSingleton('adminhtml/session')->addSuccess(
$this->__('Your event has been saved')
);
}catch(Exception $e){
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
$this->_redirect('*/*/index');
}
}
}
和Form.php文件:
class MasteringMagento_Example_Block_Adminhtml_Event_Edit_Form extends Mage_Adminhtml_Block_Widget_Form{
protected function _prepareForm(){
$form = new Varien_Data_Form(array('id'=>'edit_form',
'action'=>$this->getData('action'), 'method'=>'post'));
$fieldset = $form->addFieldset('base_fieldset',
array('legend'=>Mage::helper('example')->__('General Information'),
'class'=>'fieldset-wide'));
$fieldset->addField('name', 'text', array(
'name' => 'name',
'label' => Mage::helper('example')->__('Event Name'),
'title' => Mage::helper('example')->__('Event Name'),
'required' => true
));
$dateFormatIso = Mage::app()->getLocale()->getDateFormat(
Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('start', 'date', array(
'name' => 'start',
'format' => $dateFormatIso,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'label' => Mage::helper('example')->__('Start Date'),
'title' => Mage::helper('example')->__('Start Date'),
'required' => true
));
$fieldset->addField('end', 'date', array(
'name' => 'end',
'format' => $dateFormatIso,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'label' => Mage::helper('example')->__('End Date'),
'title' => Mage::helper('example')->__('End Date'),
'required' => true
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
我认为错误来自我的控制器。但是,如果我将url链接指向表单,它将显示。但如果我直接指向其容器Edit.php,则会出现上述错误:
class MasteringMagento_Example_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action{
public function indexAction(){
$this->loadLayout();
$this->_addContent(
$this->getLayout()->createBlock('example/adminhtml_event_edit'));
//go straight to the php file to render the form. otherwise this will not perfomed.
$this->renderLayout();
}}
这是我的config.xml。我确实包含了Magento块的基类:
<blocks>
<example>
<class>MasteringMagento_Example_Block</class>
</example>
</blocks>
MasteringMagento\u示例块
请帮我找出问题所在。感谢createBlock()是Magento内部块的抽象工厂模式。每当Magento无法从此方法解析工厂类时,将返回一个布尔值。。。正如错误消息所说,在您的示例中就是这种情况
检查您的类MasteringMagento\u Example\u Block\u Adminhtml\u Event\u Edit
是否存在拼写、大小写或与类相关的错误。还要确保类文件位于app/code/local/MasteringMagento/Example/Block/Adminhtml/Event/Edit.php
createBlock()
是Magento内部块的抽象工厂模式。每当Magento无法从此方法解析工厂类时,将返回一个布尔值。。。正如错误消息所说,在您的示例中就是这种情况
检查您的类
MasteringMagento\u Example\u Block\u Adminhtml\u Event\u Edit
是否存在拼写、大小写或与类相关的错误。还要确保类文件位于app/code/local/MasteringMagento/Example/Block/Adminhtml/Event/Edit.php中。在magento中,每个管理表单块首先由表单容器加载
在这里,您可以调用MasteringMagento\u示例\u块\u Adminhtml\u事件\u编辑类:
$this->getLayout()->createBlock('example/adminhtml_event_edit')
这个类位于app/code/local/MasteringMagento/Example/Block/Adminhtml/Post/Edit.php中,应该是这样的:
<?php
/**
* MasteringMagento_Example_Block_Adminhtml_Post_Edit
*/
class MasteringMagento_Example_Block_Adminhtml_Post_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
// $this->_objectId = 'id';
parent::__construct();
$this->_blockGroup = 'example';
$this->_controller = 'adminhtml_post';
$this->_mode = 'edit';
$modelTitle = $this->_getModelTitle();
$this->_updateButton('save', 'label', $this->_getHelper()->__("Save $modelTitle"));
$this->_addButton('saveandcontinue', array(
'label' => $this->_getHelper()->__('Save and Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
$this->_formScripts[] = "
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
protected function _getHelper(){
return Mage::helper('example');
}
protected function _getModel(){
return Mage::registry('exemple_youmodel');
}
protected function _getModelTitle(){
return 'Post';
}
public function getHeaderText()
{
$model = $this->_getModel();
$modelTitle = $this->_getModelTitle();
if ($model && $model->getId()) {
return $this->_getHelper()->__("Edit $modelTitle (ID: {$model->getId()})");
}
else {
return $this->_getHelper()->__("New $modelTitle");
}
}
/**
* Get URL for back (reset) button
*
* @return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/index');
}
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', array($this->_objectId => $this->getRequest()->getParam($this->_objectId)));
}
}
在magento中,每个管理表单块首先由表单容器加载
在这里,您可以调用MasteringMagento\u示例\u块\u Adminhtml\u事件\u编辑类:
$this->getLayout()->createBlock('example/adminhtml_event_edit')
这个类位于app/code/local/MasteringMagento/Example/Block/Adminhtml/Post/Edit.php中,应该是这样的:
<?php
/**
* MasteringMagento_Example_Block_Adminhtml_Post_Edit
*/
class MasteringMagento_Example_Block_Adminhtml_Post_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
// $this->_objectId = 'id';
parent::__construct();
$this->_blockGroup = 'example';
$this->_controller = 'adminhtml_post';
$this->_mode = 'edit';
$modelTitle = $this->_getModelTitle();
$this->_updateButton('save', 'label', $this->_getHelper()->__("Save $modelTitle"));
$this->_addButton('saveandcontinue', array(
'label' => $this->_getHelper()->__('Save and Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
$this->_formScripts[] = "
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
protected function _getHelper(){
return Mage::helper('example');
}
protected function _getModel(){
return Mage::registry('exemple_youmodel');
}
protected function _getModelTitle(){
return 'Post';
}
public function getHeaderText()
{
$model = $this->_getModel();
$modelTitle = $this->_getModelTitle();
if ($model && $model->getId()) {
return $this->_getHelper()->__("Edit $modelTitle (ID: {$model->getId()})");
}
else {
return $this->_getHelper()->__("New $modelTitle");
}
}
/**
* Get URL for back (reset) button
*
* @return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/index');
}
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', array($this->_objectId => $this->getRequest()->getParam($this->_objectId)));
}
}