Magento-正在创建新的Adminhtml表单,但saveAction()不起作用
我正在使用Magento 1.9.0.1,目前正在使用自定义Magento扩展 以下是表单的屏幕截图: 当我点击Magento-正在创建新的Adminhtml表单,但saveAction()不起作用,magento,Magento,我正在使用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] => zj9E8FpNOCyFI
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();
}
}
这是我在扩展名exept adminhtml.xml和system.xml中拥有的所有文件。
如果你认为它们很重要,我会展示给你看
那么,您能看到为什么不能正确保存的问题吗?
你能帮我解决这个问题吗
提前谢谢 我猜post数据中缺少id。因此,它不会加载所需的模型数据,而是创建一个新条目。
可能,$this->getRequest()->getParam('id')
返回null
将隐藏字段id
添加到具有条目id的表单中。xpoback是正确的,如果您正在执行编辑操作,则需要在表单中添加隐藏字段:
因此,在_prepareForm()中,在Receiver字段之前添加:
$model = Mage::registry('smsnotification_data');
if ($model->getId()) {
$fieldset->addField('id', 'hidden', array(
'name' => 'id',
));
}
所以如果我创建Mage::log($this->getRequest()->getParam('id')代码>如果您的猜测正确,它应该在我的日志文件中返回null?是的。如果是这样,只需添加一个id为的隐藏字段,如下面的JonaPk示例所示。@TonyStark,关于db,在本例中不重要,imho。如果添加了此字段,请确保在html源代码中已从模型中填充了该字段的值,并将其以POST方式发送到控制器。如何检查它是否将其发送到控制器?1。使用调试器2。在控制器中制作print\r($\u POST)
。我想这可能不是问题所在。根据您的建议查看表单文件:但它仍然没有进行更改。再看看这个答案:你认为这也可能是个问题吗?是的,它也可能是个问题。。。您应该始终使用小写字母(字段名、表名等)编写数据库关系,因为Magento ORM进行从下划线到camelcase的转换,反之亦然。。。所以这可能是个问题,而且调试起来很困难,所以您建议我将表名从VivasIndustries\u SmsNotification
更改为VivasIndustries\u SmsNotification
以及所有表行名?不,这是不必要的,因为对于表名,magento将在config.xml中查找,您应该系统地执行此操作,这是一个需要遵循的约定(最佳实践),但是对于字段名,您应该执行此操作…我已经执行了此操作,并且当我尝试通过点击Save Item
->SQLSTATE[42000]提交更改时出现此错误:语法错误或访问冲突:1064您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解第1行“WHERE(id='13')”附近使用的正确语法
您能给我一些建议吗?
<?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');
}
}
}
<?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();
}
}
<?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()
);
}
}
<?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>
<?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');
}
}
<?php
class VivasIndustries_SmsNotification_Model_Resource_Smsnotification_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{
protected function _construct(){
$this->_init('smsnotification/smsnotification');
}
}
<?php
class VivasIndustries_SmsNotification_Model_Smsnotification extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('smsnotification/smsnotification');
}
}
<?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();
?>
$model = Mage::registry('smsnotification_data');
if ($model->getId()) {
$fieldset->addField('id', 'hidden', array(
'name' => 'id',
));
}