Magento's magic setter only updated some db columns

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?

2 thoughts on “Magento's magic setter only updated some db columns”

  1. 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.

  2. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *