Presuming you have set up all your Models successfully. You can do this using your own source model.
Create a file at Yourmodule/Namespace/Model/Source/Distributor.php
Inside this you need to extend Mage_Eav_Model_Entity_Attribute_Source_Abstract
You will then want to create your Options array for the dropdown, we do this by implementing the getAllOptions()
method like so:
public function getAllOptions()
{
$distributors = Mage::getModel('mymodule/distributors')->getCollection()
->setOrder('distributor_name', 'ASC');
$options = array(
NULL => '- Please Select -'
);
foreach ($distributors as $distributor) {
$options [$distributor->getId()] = $distributor->getDistributorName();
}
return $options;
}
This will construct your option array used for the select box when setting the product attribute in the admin.
Along with getAllOptons()
you will want to implement getFlatColums()
and also getFlatUpdateSelect($store)
in order for Magento to include the attribute in the flat table. You can do this like so:
public function getFlatColums()
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$column = array(
'unsigned' => false,
'default' => null,
'extra' => null
);
if (Mage::helper('core')->useDbCompatibleMode()) {
$column['type'] = 'VARCHAR';
$column['is_null'] = true;
} else {
$column['type'] = Varien_Db_Ddl_Table::TYPE_VARCHAR;
$column['nullable'] = true;
$column['comment'] = $attributeCode . ' column';
}
return array($attributeCode => $column);
}
public function getFlatUpdateSelect($store) {
return Mage::getResourceModel('eav/entity_attribute')->getFlatUpdateSelect($this->getAttribute(), $store);
}
Now you've created your source model for your attribute you will want to install it. We can do this with an install script, if you have created your own custom entity i am assuming you are handy with Install Scripts so i'll leave that to you. Install it as you would any other product attribute, however when you do make sure you set the following:
'input' => 'select', //frontend input
'source => 'mymodule/source_distributor' //your source model
Once installed, make sure your attribute is assigned to the correct attribute set, then edit the product and you should see your dropdown, populated with all your distributors.
For a multiselect, simply change the input specified in the install script to multiselect
.
If you need any further reading on this subject take a look at this Inchoo post on creating a custom attribute source type:
Creating a custom attribute source
Hope this helps. Any questions, just ask.
Josh.