Magento – Creating a Customer Attribute set by Dropdown Populated from a table

customer-attributedropdown-attribute

I have a custom module that created the table Namespace_Module_Rep_Directory_Rep. In this table we have a set of locations with entity_id, name, email, phone, status.

I want to be able to assign each customer to a rep. I know how to make customer attributes, but I am unsure how to make one that is linked to this table. I want to be able to go to a customer, and for "Rep" select a rep from a dropdown menu populated with the names of the reps from Namespace_Module_Rep_Directory_Rep.

I have looked all over for tutorials or similar setups, but have yet to find anything really useful. If anyone can provide some insight on how to accomplish this or a site that may help me I'd appreciate it, thanks.


Update:

I have followed @Marius's answer and have run into a problem implementing it. When I go 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, which @Marius provided.

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

You just need an attribute with a custom source model.
Here is an example on how to create an attribute with custom options. It's for products but the idea is similar. You said you know how to create attributes. Well...do your magic and just fill in the source setting of the attribute with your model alias.
The model that you use as source must contain a method getAllOptions that should return an array like this:

array(
    array('label' => 'Label 1', 'value' => 'value 1'),
    array('label' => 'Label 2', 'value' => 'value 2'),
    array('label' => 'Label 3', 'value' => 'value 3'),
    ....
)

The label will be show in the dropdown and the value will be saved in the db.

In the example I linked there are 2 method in the source model that you can skip. They are needed only for products and categories. getFlatColums and getFlatUpdateSelect.