tags:

views:

342

answers:

2

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_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>

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
+1  A: 

looks like you are missing commit command.

->save()

..or something

Imre L
I tried adding `$writeconnection->save();` immediately after `$writeconnection->query($sql);`, but that caused the codfe to stop running at that point. I can only assume that there was an error in the code, but I have been unable to trap the specific issue. As a result I am positive that `->save()` only works when using Magento's built in functions rather than the direct SQL method as I am using. Besides, no example (including the core modules) uses `->save()` when using `->query()`. Thanks for the suggestion though.
Joseph
@Imre L: I have tried switching to the EAV model as well, but it does not appear to work either giving the same issue as mentioned above.
Joseph
I went ahead and accepted this one as the answer even though the detail was not perfect as the theory was still correct. My solution is posted as an answer as well.
Joseph
Thanks. You are using InnoDB which is one of the few transactional storage engines in MySQL. I'm not familiar with Magento itself.
Imre L
A: 

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.

Joseph