Magento – Fix ‘Source Model Not Found for Attribute’ Error

customer-attributeeav

Edit: For anyone who may also have this problem, make sure that your spelling is correct, but also that your capitalization is correct. And remember that magento cannot read model files that have any capitalization other than at the start. Aka Testvariable.php works but TestVariable.php will cause it to fail to find.

I have created a custom customer attribute (using generic names for this question) called module in custom extenstion namespace/module. The select attribute is created, but the options for it are blank. If I try to edit a customer, I get the error:

"Source model "namespace/attribute_source_module" not found for attribute "module"

It seems to be unable to find the model with the options for the select, so it errors out. I followed the example here to create the select attribute, adapted (hopefully correctly) to be used for a customer attribute instead of a product attribute. The following is all the code for the extension. I have quadruple checked spelling and things and everything is laid out correctly, so maybe I'm missing something needed to make it a customer attribute? I'm on magento EE 1.14.0.1, which is essentially the same as CE 1.9.0.1.

Namespace/Module/etc/config:

...
    <global>
        <models>
            <module>
                <class>Namespace_Module_Model</class>
            </module>
        </models>

...

Namespace/Module/Model/Attribute/Source/Module.php:

<?php
class Namespace_Module_Model_Attribute_Source_Module extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;
    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();

            $this->_options[] = array('label'=> $this->__('Option 1'), value=>1);
            $this->_options[] = array('label'=> $this->__('Option 2'), value=>3);
            $this->_options[] = array('label'=> $this->__('Option 3'), value=>3);
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}

Namespace/Module/sql/module_steup/install-0.1.0.php

<?php
$installer = $this;

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute("customer", "module",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Module",
    "input"    => "select",
    "source"   => "module/attribute_source_module",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Module"

        ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "module");


$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'module',
    '999'  //sort_order
);

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
//$used_in_forms[]="customer_account_edit";
//$used_in_forms[]="adminhtml_checkout";
        $attribute->setData("used_in_forms", $used_in_forms)
                ->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1)
                ->setData("sort_order", 100)
                ;
        $attribute->save();



$installer->endSetup();

Best Answer

For those who might have this issue, I solved it by another way. It happened when I disabled a module and some attributes were still trying to be launched in db, even if they were useless.

what i did is this :

delete from eav_attribute where source_model = 'my_source_model'