2016-05-13 5 views
0

私はmangetoフレームワークを初めて使用しています。管理領域セクションにフォームを作成する方法を学んでいます。しかし、私が受けています、私はエラーを見つけ出すことができなかった時間の上にされています:管理領域Magentoに設定してもフォームに保存ボタンが表示されない

Recoverable Error: Argument 1 passed to Mage_Adminhtml_Controller_Action::_addContent() must be an instance of Mage_Core_Block_Abstract, boolean given, called in /vagrant/magento/app/code/local/MasteringMagento/Example/controllers/Adminhtml/EventController.php on line 12.

を以下の私の个人设定が私のForm.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> 

問題を特定するのを手伝ってください。ありがとう

答えて

0

createBlock()は、Magento内部のブロックの抽象的な工場パターンです。 Magentoがこのメソッドからファクトリクラスを解決できないときはいつでも、ブール値が返されます。エラーメッセージにはあなたの例の場合があります。

クラスMasteringMagento_Example_Block_Adminhtml_Event_Editのスペル、ケーシング、またはクラスに関連するエラーを確認してください。また、クラスファイルがapp/code/local/MasteringMagento/Example/Block/Adminhtml/Event/Edit.phpにあることを確認してください。

0

magentoでは、すべての管理フォームブロックが最初にフォームコンテナによって読み込まれます。ここで

、あなたはMasteringMagento_Example_Block_Adminhtml_Event_Editクラスを呼び出す:

$this->getLayout()->createBlock('example/adminhtml_event_edit') 

このクラス、アプリ/コードの中に配置/ローカル/ MasteringMagento /例/ブロック/ Adminhtml//个人设定ポストは、のようなものになりますこのように:

<?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))); 
    } 

} 

ご覧のとおり、すべてのボタンは__construct()メソッドで設定されています。

希望します。

関連する問題