【问题标题】:Magento - Data is not inserted into database, but the id is autoincrementedMagento - 数据未插入数据库,但 id 自动递增
【发布时间】:2010-06-01 22:02:00
【问题描述】:

我正在为 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

【问题讨论】:

  • 请同时发布表格定义...
  • @Zak:感谢您的快速回复。表格def已发布。

标签: php mysql magento


【解决方案1】:

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

感谢您为此提供的帮助。

【讨论】:

    【解决方案2】:

    看起来您缺少提交命令。

    ->save()
    

    ..什么的

    【讨论】:

    • 我尝试在$writeconnection-&gt;query($sql); 之后立即添加$writeconnection-&gt;save();,但这导致codfe 在那时停止运行。我只能假设代码中存在错误,但我一直无法捕捉到具体问题。因此,我很肯定-&gt;save() 仅在使用 Magento 的内置函数而不是我使用的直接 SQL 方法时才有效。此外,在使用-&gt;query() 时,没有示例(包括核心模块)使用-&gt;save()。不过感谢您的建议。
    • 我也尝试过切换到 EAV 模型,但它似乎也不起作用,也出现了与上述相同的问题。
    • 我继续接受这个作为答案,尽管细节并不完美,因为理论仍然正确。我的解决方案也作为答案发布。
    • 谢谢。您正在使用 InnoDB,它是 MySQL 中为数不多的事务存储引擎之一。我对 Magento 本身并不熟悉。
    猜你喜欢
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多