我拥有所有必要的文件和结构,以便在我添加到数据库中的表上处理 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 代码,我按照@benmarks 的建议Mage::register('allpages_date')
在调用之前调用renderLayout()
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();
}