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();
并且这工作得很好,直到我添加了提供程序和 profile_name 列,这些列永远不会更新:
Mage::getModel('module/identifiers')
->setIdentifier($profile['identifier'])
->setProvider($profile['provider'])
->setProfileName($profile['profile_name'])
->setCustomerId($customer_id)
->save();
我删除了自定义表,从 core_resource 列表中删除了模块的设置脚本,以便它将再次运行(它确实这样做了),并且新行被添加到数据库中,但他们缺少我的新列的数据。是的,我已经验证我的 $profile 数组具有正确的数据。我做错了什么?
I have a custom mysql table we'll call module_identifiers
and a corresponding model in my module called Identifiers. This is my setup script:
$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();
In my Module/Model/Identifiers.php
class Namespace_Module_Model_Identifiers extends Mage_Core_Model_Abstract {
protected function _construct() {
$this->_init('module/identifiers');
}
}
And in 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');
}
}
So when I want to write to this table, I've been doing this:
Mage::getModel('module/identifiers')
->setIdentifier($profile['identifier'])
->setCustomerId($customer_id)
->save();
And that was working great until I added the provider and profile_name columns, and those are never updating with this:
Mage::getModel('module/identifiers')
->setIdentifier($profile['identifier'])
->setProvider($profile['provider'])
->setProfileName($profile['profile_name'])
->setCustomerId($customer_id)
->save();
I dropped my custom table, removed my module's setup script from the core_resource list so it would run again (which it did), and the new rows are added to the database, but they're missing the data for my new columns. Yes, I've verified that my $profile array has the correct data. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果有人最终在这里寻找这个非常令人沮丧的问题的答案,那就是这个。
Magento 将其表描述缓存在 var/cache 中(即使您没有在后端打开任何缓存)(请参阅 lib/Varien/Db/Adapter/Pdo/Mysql.php::describeTable())。当它要插入或更新任何表时,它首先检查此缓存以查看是否有准备好的表的描述。如果找到一个,它会返回表的缓存描述,并且如果传递给 $model->save() 的数组中的任何字段未出现在此缓存描述中,则它们不会被插入到表,导致上述症状。
因此,如果您正在摆弄在 phpMyAdmin 中插入列(或者甚至重新运行您的设置脚本),Magento 将看不到它们,也不会插入您的数据。
极其简单的解决方案就是简单地执行 rm -rf var/cache/*
编辑 2014.01.10:如果您的站点使用内存缓存,则还需要清除此缓存才能进行更改影响。
If anyone ends up here looking for an answer to this very frustrating problem it is this.
Magento caches its table descriptions in var/cache (even if you don't have any caches switched on the backend) (see lib/Varien/Db/Adapter/Pdo/Mysql.php::describeTable()). When it goes to insert or update any table, it first checks this cache to see if it has a description of the table ready to go. If it finds one, it returns the cached description of the table, and if any of the fields in the array you pass to $model->save() don't appear in this cached description, they won't get inserted into the table, leading to the symptoms described above.
So if you are fiddling about inserting columns in phpMyAdmin (or even re-running your setup script), Magento won't see them and won't insert your data.
The absurdly simple solution is simply to do a
rm -rf var/cache/*
EDIT 2014.01.10: If your site makes use of memcache, this cache will need to be cleared as well for changes to take effect.
通过知识库模型文章,它将引导您完成设置基本模型所需的每个步骤。然后,单独测试您的单个模型,以确保其正确保存/设置,然后将该工作集成到模块的其余部分中。
Work your way through the knowledge base Models article, which will run you through every step you need to take to setup a basic Model. Then, test your individual model in isolation to ensure it's saving/setting correctly, and then integrate that work into the rest of your module.