我正在使用Magento 1.9.0.1,目前我正在使用自定义Magento扩展程序!
以下是表单的屏幕截图:
当我点击Save Item
时,我得到了这个:
正如您所见,saveAction()
尚未保存更改。
当我放置Mage::log($this->getRequest()->getPost());
以查看表单是否正常工作时,我得到了这个:
2015-02-17T13:25:08+00:00 DEBUG (7): Array
(
[form_key] => zj9E8FpNOCyFIqaT
[Receiver] => Veni
[Phone] => 359884685063
[Date] => 2015-02-05 19:06:44
)
通过这个我认为形式是好的。
让我向您展示我在扩展程序中的所有代码!
我在:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status.php:
<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
$this->_controller = 'adminhtml_sms_status';
$this->_blockGroup = 'smsnotification';
$this->_headerText = Mage::helper('smsnotification')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('smsnotification')->__('Add Item');
parent::__construct();
}
}
我有:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Grid.php:
<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('smsnotification_grid');
$this->setDefaultSort('id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('smsnotification/smsnotification_collection');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('id', array(
'header' => Mage::helper('smsnotification')->__('ID'),
'align' =>'right',
'width' => '50px',
'index' => 'id',
));
$this->addColumn('Receiver', array(
'header' => Mage::helper('smsnotification')->__('Receiver'),
'align' =>'left',
'index' => 'Receiver',
));
$this->addColumn('Phone', array(
'header' => Mage::helper('smsnotification')->__('Phone'),
'align' =>'left',
'index' => 'Phone',
));
$this->addColumn('Date', array(
'header' => Mage::helper('smsnotification')->__('Date'),
'align' =>'left',
'index' => 'Date',
));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
}
}
我在:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit.php:
<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'smsnotification';
$this->_controller = 'adminhtml_sms_status';
$this->_updateButton('save', 'label', Mage::helper('smsnotification')->__('Save Item'));
$this->_updateButton('delete', 'label', Mage::helper('smsnotification')->__('Delete Item'));
}
public function getHeaderText()
{
if( Mage::registry('smsnotification_data') && Mage::registry('smsnotification_data')->getId() ) {
return Mage::helper('smsnotification')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('smsnotification_data')->getReceiver()));
} else {
return Mage::helper('smsnotification')->__('Add Item');
}
}
}
我在:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit/Form.php:
<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
));
$fieldset = $form->addFieldset('edit_form', array('legend'=>Mage::helper('smsnotification')->__('Item information')));
$fieldset->addField('Receiver', 'text', array(
'label' => Mage::helper('smsnotification')->__('Receiver'),
'class' => 'required-entry',
'required' => true,
'name' => 'Receiver',
));
$fieldset->addField('Phone', 'text', array(
'label' => Mage::helper('smsnotification')->__('Phone'),
'class' => 'required-entry',
'required' => true,
'name' => 'Phone',
));
$fieldset->addField('Date', 'text', array(
'label' => Mage::helper('smsnotification')->__('Date'),
'class' => 'required-entry',
'required' => true,
'name' => 'Date',
));
if ( Mage::getSingleton('adminhtml/session')->getsmsnotificationData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getsmsnotificationData());
Mage::getSingleton('adminhtml/session')->setsmsnotificationData(null);
} elseif ( Mage::registry('smsnotification_data') ) {
$form->setValues(Mage::registry('smsnotification_data')->getData());
}
// Add these two lines
$form->setUseContainer(true);
$this->setForm($form);
////
return parent::_prepareForm();
}
}
我在:/app/code/community/VivasIndustries/SmsNotification/controllers/Adminhtml/SmsorderstatusesController.php:
<?php
class VivasIndustries_SmsNotification_Adminhtml_SmsorderstatusesController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->_title($this->__('SMS Center'))->_title($this->__('SMS Center'));
$this->loadLayout();
$this->_setActiveMenu('vivassms');
$this->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status'));
$this->renderLayout();
}
public function editAction()
{
$smsnotificationId = $this->getRequest()->getParam('id');
$smsnotificationModel = Mage::getModel('smsnotification/smsnotification')->load($smsnotificationId);
if ($smsnotificationModel->getId() || $smsnotificationId == 0) {
Mage::register('smsnotification_data', $smsnotificationModel);
$this->loadLayout();
$this->_setActiveMenu('vivassms');
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_edit'));
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('smsnotification')->__('Item does not exist'));
$this->_redirect('*/*/');
}
}
public function newAction()
{
$this->_forward('edit');
}
public function saveAction()
{
if ( $this->getRequest()->getPost() ) {
try {
Mage::log($this->getRequest()->getPost());
$postData = $this->getRequest()->getPost();
$smsnotificationModel = Mage::getModel('smsnotification/smsnotification')->load($this->getRequest()->getParam('id'));
$smsnotificationModel->setReceiver($postData['Receiver'])
->setPhone($postData['Phone'])
->setDate($postData['Date'])
->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
Mage::getSingleton('adminhtml/session')->setsmsnotificationData(false);
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setsmsnotificationData($this->getRequest()->getPost());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
$this->_redirect('*/*/');
}
public function deleteAction()
{
if( $this->getRequest()->getParam('id') > 0 ) {
try {
$smsnotificationModel = Mage::getModel('smsnotification/smsnotification');
$smsnotificationModel->setId($this->getRequest()->getParam('id'))
->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
$this->_redirect('*/*/');
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
/**
* Product grid for AJAX request.
* Sort and filter result for example.
*/
public function gridAction()
{
$this->loadLayout();
$this->getResponse()->setBody(
$this->getLayout()->createBlock('smsnotification/adminhtml_smsnotification_grid')->toHtml()
);
}
}
我在:/app/code/community/VivasIndustries/SmsNotification/etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<VivasIndustries_SmsNotification>
<version>0.1.0</version>
</VivasIndustries_SmsNotification>
</modules>
<global>
<models>
<smsnotification>
<class>VivasIndustries_SmsNotification_Model</class>
<resourceModel>vivasindustries_smsnotification_resource</resourceModel>
</smsnotification>
<vivasindustries_smsnotification_resource>
<class>VivasIndustries_SmsNotification_Model_Resource</class>
<entities>
<smsnotification>
<table>VivasIndustries_SmsNotification</table>
</smsnotification>
</entities>
</vivasindustries_smsnotification_resource>
</models>
<resources>
<smsnotification_setup>
<setup>
<module>VivasIndustries_SmsNotification</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</smsnotification_setup>
<smsnotification_read>
<connection>
<use>core_read</use>
</connection>
</smsnotification_read>
<smsnotification_write>
<connection>
<use>core_write</use>
</connection>
</smsnotification_write>
</resources>
<events>
<sales_order_save_after>
<observers>
<vivasindustries_smsnotification>
<class>smsnotification/observer</class>
<method>orderSaved</method>
</vivasindustries_smsnotification>
</observers>
</sales_order_save_after>
</events>
<helpers>
<smsnotification>
<class>VivasIndustries_SmsNotification_Helper</class>
</smsnotification>
</helpers>
<blocks>
<smsnotification>
<class>VivasIndustries_SmsNotification_Block</class>
</smsnotification>
</blocks>
</global>
<adminhtml>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<system>
<children>
<config>
<children>
<vivas>
<title>Vivas - All</title>
</vivas>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
<layout>
<updates>
<smsnotification>
<file>smsnotification.xml</file>
</smsnotification>
</updates>
</layout>
</adminhtml>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<VivasIndustries_SmsNotification before="Mage_Adminhtml">VivasIndustries_SmsNotification_Adminhtml</VivasIndustries_SmsNotification>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
我在:/app/code/community/VivasIndustries/SmsNotification/Model/Resource/Smsnotification.php:
<?php
class VivasIndustries_SmsNotification_Model_Resource_Smsnotification extends Mage_Core_Model_Resource_Db_Abstract
{
/**
* Initialize resource model
*
* @return void
*/
public function _construct()
{
$this->_init('smsnotification/smsnotification','id');
}
}
我在:/app/code/community/VivasIndustries/SmsNotification/Model/Resource/Smsnotification/Collection.php:
<?php
class VivasIndustries_SmsNotification_Model_Resource_Smsnotification_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{
protected function _construct(){
$this->_init('smsnotification/smsnotification');
}
}
我在:/ app / code / community / VivasIndustries / SmsNotification / Model /
<?php
class VivasIndustries_SmsNotification_Model_Smsnotification extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('smsnotification/smsnotification');
}
}
我在:/app/code/community/VivasIndustries/SmsNotification/sql/smsnotification_setup/install-1.0.0.php:
<?php
$installer=$this;
$installer->startSetup();
$installer->run("
CREATE TABLE IF NOT EXISTS `VivasIndustries_SmsNotification` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Receiver` varchar(500) NOT NULL,
`Phone` varchar(500) NOT NULL,
`Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
");
$installer->endSetup();
?>
这是我在Extension中的所有文件,exept adminhtml.xml和system.xml。 如果您认为展示它们非常重要,我会展示它们。
那么你能否看到问题为什么它没有正确保存? 你能帮我解决这个问题吗?
提前致谢!
答案 0 :(得分:2)
我的猜测是帖子数据中缺少id。因此它不会加载所需的模型数据,而是创建一个新条目。
可能$this->getRequest()->getParam('id')
返回null。
使用条目ID为您的表单添加隐藏字段id
。
答案 1 :(得分:0)
xpoback是正确的,如果您正在编辑操作,则需要在表单中添加隐藏字段:
所以在_prepareForm()中,在Receiver字段之前添加:
$model = Mage::registry('smsnotification_data');
if ($model->getId()) {
$fieldset->addField('id', 'hidden', array(
'name' => 'id',
));
}