所以我正在尝试检查自定义数据库表,如果'test_type'列中存在'something',则用新数据替换现有行.但是,它仍在继续在数据库表中写入重复的列值'test_type'.所以我最终在'test_type'列中有两行'something'.
$adapter = Mage::getSingleton('core/resource')->getConnection('core_write');
$data = array(
'test_type' => 'something',
'time_stamp' => $timeStamp,
);
$adapter->insertOnDuplicate('test_table', $data, array('test_type'));
Run Code Online (Sandbox Code Playgroud)
以下是用于创建表的更新脚本.
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$installer->getTable('test_table')};
CREATE TABLE `{$installer->getTable('test_table')}` (
`auto_id` int(10) NOT NULL auto_increment,
`test_type` varchar(50) NOT NULL,
`time_stamp` varchar(50) NOT NULL,
PRIMARY KEY (`auto_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
Run Code Online (Sandbox Code Playgroud)
为了使数据库理解重复行以表示与test_type现有行中相同的行,您可以test_type在唯一索引中添加列,例如
UNIQUE KEY `test_type_idx` (`test_type`)
Run Code Online (Sandbox Code Playgroud)
从而改变你的表创建代码;
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$installer->getTable('test_table')};
CREATE TABLE `{$installer->getTable('test_table')}` (
`auto_id` int(10) NOT NULL auto_increment,
`test_type` varchar(50) NOT NULL,
`time_stamp` varchar(50) NOT NULL,
PRIMARY KEY (`auto_id`)
UNIQUE KEY `test_type_idx` (`test_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
Run Code Online (Sandbox Code Playgroud)
编辑
另一个问题是将第三个参数传递给insertOnDuplicate方法.如果存在重复,则应指定需要更新的字段,因此可能是这样time_stamp.
$adapter->insertOnDuplicate('test_table', $data, array('time_stamp'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4855 次 |
| 最近记录: |