Php Magento-数据不会插入数据库,但id会自动递增
我正在为Magento开发一个新的支付模块,遇到了一个无法解释的问题。验证信用卡后运行的以下代码:Php Magento-数据不会插入数据库,但id会自动递增,php,mysql,magento,Php,Mysql,Magento,我正在为Magento开发一个新的支付模块,遇到了一个无法解释的问题。验证信用卡后运行的以下代码: $table_prefix = Mage::getConfig()->getTablePrefix(); $tableName = $table_prefix.'authorizecim_magento_id_link'; $resource = Mage::getSingleton('core/resource'); $writeconnection =
$table_prefix = Mage::getConfig()->getTablePrefix();
$tableName = $table_prefix.'authorizecim_magento_id_link';
$resource = Mage::getSingleton('core/resource');
$writeconnection = $resource->getConnection('core_write');
$acPI = $this->_an_customerProfileId;
$acAI = $this->_an_customerAddressId;
$acPPI = $this->_an_customerPaymentProfileId;
$sql = "insert into {$tableName} values ('', '$customerId', '$acPI', '$acPI', '3')";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acAI', '2')";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acPPI', '1')";
$writeconnection->query($sql);
我已经使用Firebug和FireHP验证了SQL查询在语法上是正确的,并且没有返回错误
奇怪的是,我检查了数据库,每次运行代码时,autoincrement值都会递增。但是,不会在数据库中插入任何行。我通过添加die()验证了这一点代码>语句直接在第一次写入之后
你知道为什么会这样吗
config.xml的相对部分如下所示:
<config>
<global>
<models>
<authorizecim>
<class>CPAP_AuthorizeCim_Model</class>
</authorizecim>
<authorizecim_mysql4>
<class>CPAP_AuthorizeCim_Model_Mysql4</class>
<entities>
<anlink>
<table>authorizecim_magento_id_link</table>
</anlink>
</entities>
<entities>
<antypes>
<table>authorizecim_magento_types</table>
</antypes>
</entities>
</authorizecim_mysql4>
</models>
<resources>
<authorizecim_setup>
<setup>
<module>CPAP_AuthorizeCim</module>
<class>CPAP_AuthorizeCim_Model_Resource_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</authorizecim_setup>
<authorizecim_write>
<connection>
<use>core_write</use>
</connection>
</authorizecim_write>
<authorizecim_read>
<connection>
<use>core_read</use>
</connection>
</authorizecim_read>
</resources>
</global>
</config>
看起来您缺少提交命令
->save()
…或其他什么在深入研究代码并搜索所有内容后,我意识到Magento使用事务模型进行数据库连接。因此,Imre L
的想法是正确的,但代码是错误的
作为实验,我更改了以下代码:
$sql = "insert into {$tableName} values ('', '$customerId', '$acPI', '$acPI', '3')";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acAI', '2')";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acPPI', '1')";
$writeconnection->query($sql);
为此:
$sql = "insert into {$tableName} values ('', '$customerId', '$acPI', '$acPI', '3'); commit;";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acAI', '2'); commit;";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acPPI', '1'); commit;";
$writeconnection->query($sql);
令人惊讶的是,它成功了。新值出现在数据库中。(我没有意识到MySQL支持事务)
在我的新代码中的某个地方,我阻止了commit语句的运行,因此没有将值保存到数据库中。我会一路追查,但现在,commit代码>将不得不留下
感谢您在这方面的帮助。请同时发布表格def…@Zak:谢谢您的快速回复。表def已发布。我尝试添加$writeconnection->save()
紧跟在$writeconnection->query($sql)之后代码>,但这导致codfe在该点停止运行。我只能假设代码中有错误,但我无法捕获特定问题。因此,我确信->save()
仅在使用Magento的内置函数而不是我使用的直接SQL方法时有效。此外,在使用->query()
时,没有示例(包括核心模块)使用->save()
。不过,谢谢你的建议。我也尝试过切换到EAV模型,但它似乎也不起作用,给出了与上述相同的问题。我继续并接受了这一个作为答案,尽管细节并不完美,因为理论仍然正确。我的解决方案也作为答案发布。谢谢。您正在使用InnoDB,它是MySQL中为数不多的事务存储引擎之一。我对Magento本身并不熟悉。
$sql = "insert into {$tableName} values ('', '$customerId', '$acPI', '$acPI', '3'); commit;";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acAI', '2'); commit;";
$writeconnection->query($sql);
$sql = "insert into {$tableName} (magCID, anCID, anOID, anObjectType) values ('$customerId', '$acPI', '$acPPI', '1'); commit;";
$writeconnection->query($sql);