php - 在 magento 中插入或更新

标签 php mysql magento

我在 magento 中像这样在表中插入行:

$table = Mage::getSingleton('core/resource')->getTableName('table_name');

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->insertMultiple($table,$rows);

$rows 是我插入其二维数组的数据,我有一个主键 'entity_id' 和另一列 order_id具有唯一约束。

在插入之前我想检查 order_id 是否存在于表中,如果有一行带有 order_id 然后更新其他明智的插入。

我发现 insertMultiple 如果我插入重复的 order_id 会抛出错误。如何根据表中的唯一约束列进行插入或更新。谢谢

编辑:

我需要一些想法:

INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;

最佳答案

看看 Varien_Db_Adapter_Pdo_Mysql::insertOnDuplicate():

/**
 * Inserts a table row with specified data.
 *
 * @param mixed $table The table to insert data into.
 * @param array $data Column-value pairs or array of column-value pairs.
 * @param array $fields update fields pairs or values
 * @return int The number of affected rows.
 * @throws Zend_Db_Exception
 */
public function insertOnDuplicate($table, array $data, array $fields = array())

您必须单独插入每一行,但您可以定义重复键的处理方式。我认为在你的情况下你想要更新行,所以你只需要 $table$data 作为参数。

关于php - 在 magento 中插入或更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28606321/

相关文章:

javascript - Magento jQuery 冲突 Swipebox

image - Magento - 如何检索捆绑的选项图像

php - 在 PHP 中转换日期时出现 "Call to a member function format() on a non-object"

Python 将字符串转换为类

php - 平面树数组转换成MYSQL where子句,兄弟代表OR,后代代表AND

sql - mysql 如何不更新日期时间?

javascript - 通过在文本框中输入多个产品 ID 将多个产品添加到购物车

PHP:死掉还是检查结果?

php - 在 PHP 中创建一个 Catch-All 处理程序?

php - 使用php将表单数据输入Mysql数据库