php - Magento 的 magic setter 只更新了一些数据库列

标签 php mysql magento

我有一个自定义的 mysql 表,我们将调用 module_identifiers,在我的模块中有一个对应的模型称为 Identifiers。这是我的设置脚本:

$installer = $this;

$installer->startSetup();
$installer->run("
CREATE TABLE `{$installer->getTable('module_identifiers')}` (
  `module_identifier_id` int(11) NOT NULL AUTO_INCREMENT,
  `identifier` varchar(255) NOT NULL,
  `customer_id` int(11) unsigned NOT NULL,
  `profile_name` varchar(100) NOT NULL,
  `provider` varchar(50) NOT NULL,
  PRIMARY KEY (`module_identifier_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
");
$installer->endSetup();

在我的 Module/Model/Identifiers.php 中

class Namespace_Module_Model_Identifiers extends Mage_Core_Model_Abstract {

    protected function _construct() {
        $this->_init('module/identifiers');
    }

}

在 Module/Model/Mysql4/Identifiers.php 中

class Namespace_Module_Model_Mysql4_Identifiers extends Mage_Core_Model_Mysql4_Abstract {

    protected function _construct() {
        $this->_init('module/identifiers', 'module_identifier_id');
    }

}

所以当我想写这个表的时候,我一直在这样做:

Mage::getModel('module/identifiers')
    ->setIdentifier($profile['identifier'])
    ->setCustomerId($customer_id)
    ->save();

在我添加 provider 和 profile_name 列之前,这一直很好用,而且这些列永远不会更新:

Mage::getModel('module/identifiers')
    ->setIdentifier($profile['identifier'])
    ->setProvider($profile['provider'])
    ->setProfileName($profile['profile_name'])
    ->setCustomerId($customer_id)
    ->save();

我删除了我的自定义表,从 core_resource 列表中删除了我的模块的设置脚本,以便它再次运行(它确实这样做了),并且新行被添加到数据库中,但是它们缺少我的新列的数据.是的,我已验证我的 $profile 数组具有正确的数据。我做错了什么?

最佳答案

如果有人最终来到这里寻找这个非常令人沮丧的问题的答案,那就是这个。

Magento 将其表描述缓存在 var/cache 中(即使您没有在后端打开任何缓存)(参见 lib/Varien/Db/Adapter/Pdo/Mysql.php::describeTable())。当它开始插入或更新任何表时,它首先检查此缓存以查看它是否具有准备好使用的表的描述。如果它找到一个,它返回表的缓存描述,如果你传递给 $model->save() 的数组中的任何字段没有出现在这个缓存描述中,它们就不会被插入到表,导致上述症状。

因此,如果您在 phpMyAdmin 中插入列(或者甚至重新运行您的设置脚本),Magento 将看不到它们,也不会插入您的数据。

荒谬的简单解决方案就是简单地执行 rm -rf var/cache/*

编辑 2014.01.10:如果您的站点使用内存缓存,则还需要清除此缓存才能使更改生效。

关于php - Magento 的 magic setter 只更新了一些数据库列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5224581/

相关文章:

php - 拉拉维尔 5.5 : Authorization with pivot table

php - Magento 1.7.0.2 安装重定向循环 index.php/install

mysql - Spring 数据 JPA; save() 自增主键错误

mysql - 使用用户搜索参数生成 MySQL JSON_EXTRACT 条件

mysql - 更新 Magento 数据库以反射(reflect)默认账单地址

image - Magento 图像未显示在前端

php - js 相当于 php 的 foreach($arr as $k=>$v)

php - 试图调用名为 "getpreReady"的未定义方法 (Twig & Symfony3)

mysql - mySQL 中的并发插入

php - Magento 1.9 - 产品未添加到已注销用户的购物篮中