Magento - 数据未插入数据库,但id是自动增量

时间:2010-06-01 22:02:00

标签: php mysql magento

我正在为Magento开发一个新的支付模块,并遇到了一个我无法解释的问题。验证信用卡后运行的以下代码:

    $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和FirePHP验证了SQL查询语法正确并且没有返回错误。

这里奇怪的是我检查了数据库,并且每次运行代码时自动增量值都会增加。但是,数据库中不会插入任何行。我通过在第一次写入后直接添加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>

修改 用于创建表的查询是:

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

2 个答案:

答案 0 :(得分:2)

在深入研究代码并搜索所有内容后,我意识到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;必须留下来。

感谢您提供帮助。

答案 1 :(得分:1)

看起来你缺少commit命令。

->save()

..或其他什么