我拥有所有必要的文件和结构,以便在我在数据库中添加的表上处理CRUD操作。在Edit.php
文件中,我有getHeaderText()
函数,用于修改在修改或向表中添加新值集时将出现在编辑页面上的标题。
我遇到的问题是,在评估以下代码时:
if (Mage::registry('allpages_data') && Mage::registry('allpages_data')->getId())
它始终返回NULL
,因此始终在标题上显示新引用文本。选中并在我正在编辑时正确编辑当前的值集,并在添加新值时添加新集。尝试了几种解决方案,但都没有。
第二次编辑
这是我的allpages_data
var_dump:
object(Dts_Allpages_Model_Referencedpages)[412]
protected '_eventPrefix' => string 'core_abstract' (length=13)
protected '_eventObject' => string 'object' (length=6)
protected '_resourceName' => string 'allpages/referencedpages' (length=24)
protected '_resource' => null
protected '_resourceCollectionName' => string 'allpages/referencedpages_collection' (length=35)
protected '_cacheTag' => boolean false
protected '_dataSaveAllowed' => boolean true
protected '_isObjectNew' => null
protected '_data' =>
array
empty
protected '_hasDataChanges' => boolean false
protected '_origData' => null
protected '_idFieldName' => string 'referencedpages_id' (length=18)
protected '_isDeleted' => boolean false
protected '_oldFieldsMap' =>
array
empty
protected '_syncFieldsMap' =>
array
empty
以下是我的 Edit.php 代码文件,其中包含getHeaderText
函数:
<?php
class Dts_Allpages_Block_Adminhtml_Allpagesbackend_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct(){
parent::__construct();
$id = $this->getRequest()->getParam('id');
$this->_objectId = 'id';
$this->_blockGroup = 'allpages';
$this->_controller = 'adminhtml_allpagesbackend';
$this->_mode = 'edit';
$this->_addButton('save_and_continue', array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
$this->_updateButton('save', 'label', Mage::helper('allpages')->__('Save reference'));
$this->_formScripts[] = "
function toggleEditor() {
if (tinyMCE.getInstanceById('form_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'edit_form');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'edit_form');
}
}
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
public function getHeaderText()
{
if (Mage::registry('allpages_data') && Mage::registry('allpages_data')->getId()){
return Mage::helper('allpages')->__('Edit reference');
} else {
return Mage::helper('allpages')->__('New reference');
}
}
protected function _prepareLayout()
{
if ($this->_blockGroup && $this->_controller && $this->_mode) {
$this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));
}
return parent::_prepareLayout();
}
}
这是我的 Form.php 文件代码:
<?php
class Dts_Allpages_Block_Adminhtml_Allpagesbackend_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$id = $this->getRequest()->getParam('id');
$params = array('id' => $this->getRequest()->getParam('id'));
if (Mage::getSingleton('adminhtml/session')->getdata())
{
$data = Mage::getSingleton('adminhtml/session')->getdata();
Mage::getSingleton('adminhtml/session')->getdata(null);
}
elseif (Mage::registry('allpages_data'))
{
$data = Mage::registry('allpages_data')->getdata();
}
else
{
$data = array();
}
// $entireregistryArray = Mage::getRegistry();
// var_dump($entireregistryArray);
$form = new Varien_Data_Form(
array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', $params),
'method' => 'post',
'enctype' => 'multipart/form-data',
)
);
$form->setUseContainer(true);
$this->setForm($form);
$fieldset = $form->addFieldset('allagesbackend_form', array(
'legend' =>Mage::helper('allpages')->__('References pages Information')
)
);
$fieldset->addField('current_url', 'text', array(
'label' => Mage::helper('allpages')->__('Url'),
'class' => 'required-entry',
'required' => true,
'name' => 'current_url',
'note' => Mage::helper('allpages')->__('The url of the reference.'),
)
);
$fieldset->addField('search_engine', 'text', array(
'label' => Mage::helper('allpages')->__('Engine'),
'class' => 'required-entry',
'required' => true,
'name' => 'search_engine',
)
);
$fieldset->addField('description', 'text', array(
'label' => Mage::helper('allpages')->__('Description'),
'class' => 'required-entry',
'required' => true,
'name' => 'description',
)
);
$form->setValues($data);
return parent::_prepareForm();
}
}
万一有用,我正在使用AlanStorm的 CommerceBug 工具。
第一次编辑
这是我的ActionController代码的一部分,我在调用Mage::register('allpages_date')
之前调用renderLayout()
,如@benmarks所建议的
public function editAction()
{
$id = $this->getRequest()->getParam('referencedpages_id', null);
$model = Mage::getModel('allpages/referencedpages');
if ($id) {
$model->load($id);
}
if ($model->getId() || $id == 0) {
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if ($data) {
$model->setData($data)->setId($id);
}
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('allpages')->__('Referencedpages table does not exist'));
$this->_redirect('*/*/');
}
Mage::register('allpages_data', $model);
$this->loadLayout();
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->renderLayout();
}
答案 0 :(得分:1)
如果您未在renderLayout()
之前的操作控制器中设置该注册表项,则不会以有用的方式设置块呈现。像任何其他变量一样考虑它 - 那里没有魔法。