Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Loading custom module data into backend attribute select box.. (Solved)
 
mweather
Member
 
Total Posts:  33
Joined:  2007-08-31
 

I’ve created a module to keep track of my sales reps using the Module Builder. I stripped out the date updated, enabled/disabled, etc and reduced it to a single field (other than id): name. That part works just fine.
I also created a custom attribute using the following:

<?php
class Alltribes_Salesrep_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
    {
    
/**
     * @return array
     */
    
public function getDefaultEntities()
    
{
        
return array(
            
'customer' => array(
                
'entity_model'      => 'customer/customer',
                
'attribute_model'   => '',
                
'table'             => 'customer/entity',
                
'attributes'        => array(
                    
'salesrep' => array(
                        
'group'             => 'Group/Tab',
                        
'label'             => 'Sales Rep',
                        
'type'              => 'varchar',
                        
'input'             => 'select',
                        
'default'           => '0',
                        
'class'             => '',
                        
'backend'           => '',
                        
'frontend'          => '',
                        
'source'            => 'salesrep/attribute_source_salesrep',
                        
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                        
'visible'           => true,
                        
'required'          => false,
                        
'user_defined'      => false,
                        
'searchable'        => false,
                        
'filterable'        => false,
                        
'comparable'        => false,
                        
'visible_on_front'  => false,
                        
'visible_in_advanced_search' => false,
                        
'unique'            => true
                    
),
               )
           ),
            
// define attributes for other model entities here
      
);
    
}
    }

That, too, works fine. It shows up on the customer edit page in the backend and saves properly. Here’s how I’m currently populating the select box:

<?php
class Alltribes_Salesrep_Model_Attribute_Source_Salesrep extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    
public function getAllOptions()
    
{
        
if (!$this->_options{
            $this
->_options = array(
                array(
                    
'value' => '',
                    
'label' => '',
                ),
                array(
                    
'value' => '1',
                    
'label' => 'Option 1',
                ),
                array(
                    
'value' => '2',
                    
'label' => 'Option 2',
                ),
                array(
                    
'value' => '3',
                    
'label' => 'Option 3',
                ),
            );
        
}
        
return $this->_options;
    
}
}

The problem is, I don’t want the options hard-coded in there. I want to load them from the salesrep table the module created.

I’ve tried several approaches and nothing has worked. I’m sure it’s something simple. Can anyone help?

Edit:

I found the solution. It was simpler than I thought. Instead of using salesrep/attribute_source_salesrep for the attribute source in the first codeblock above, I used salesrep/salesrep. Then, in app/coswe/local/Salesrep/Model/Salesrep.php, I added the following functions:

public function getAllOptions()
    
{
        
if (!$this->_options{
            $this
->_options Mage::getSingleton('salesrep/salesrep')->getSalesrepValuesForForm(truetrue);
        
}

        
return $this->_options;
    
}

    
public function getOptionText($value)
    
{
        
if (!$this->_options{
          $this
->_options $this->getAllOptions();
        
}
        
foreach ($this->_options as $option{
          
if ($option['value'== $value{
            
return $option['label'];
          
}
        }
        
return false;
    
}

    
public function getSalesrepValuesForForm($empty false$all false)
    
{
        $options 
= array();
        if (
$empty{
            $options[] 
= array(
                
'label' => '',
                
'value' => ''
            
);
        
}

        
foreach (Mage::getModel('salesrep/salesrep')->getCollection() as $salesrep{
            $options[] 
= array(
                
'label' => $salesrep->getName(),
                
'value' => $salesrep->getId(),
            );

        
}
        
return $options;
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top