Magento - Data is not inserted into database, but the id is autoincremented
I am working on a new payment module for Magento and have come across an issue that I cannot explain. The following code that runs after the credit card is verified:
$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);
I have verified using Firebug and FirePHP that the SQL queries are syntactically correct and no errors are returned.
The odd thing here is that I have checked the database, and the autoincrement value is incremented on every run of the code. However, no rows are inserted in the database. I have verified this by adding a die();
statement directly after the first write.
Any ideas why this would be occuring?
The relative portion of the config.xml is this:
<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_ma开发者_如何学JAVAgento_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>
Edit: The query to create the table as it stands is:
CREATE TABLE `mag_authorizecim_magento_id_link` (
`link_id` INT(11) NOT NULL AUTO_INCREMENT,
`magCID` INT(11) NOT NULL,
`anCID` INT(11) NOT NULL,
`anOID` INT(11) NOT NULL,
`anObjectType` INT(11) NOT NULL,
PRIMARY KEY (`link_id`)
) ENGINE=INNODB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8
After digging into the code and searching through everything, I realized that Magento uses a transaction model for the database connections. As a result, Imre L
had the right idea, but the wrong code.
As an experiment, I changed this code:
$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);
to this:
$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);
Amazingly enough, it worked. The new values appeared in the database. (I did not realize that MySQL supported transactions)
Somewhere in my new code, I was preventing the commit statement to run, and therefore the values were not getting saved to the database. I will hunt this down as I go, but for now, the commit;
will have to stay.
Thanks for the help working on this.
looks like you are missing commit command.
->save()
..or something
精彩评论