Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.26 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Better way to add attribute to collection
  2. protected function _prepareCollection()
  3. {
  4.     $collection = Mage::getResourceModel($this->_getCollectionClass());
  5.     foreach ($collection as &$object){
  6.         $customer = Mage::getModel('customer/customer')
  7.             ->setId($object->getCustomerId())
  8.             ->load();
  9.         $object->setCampaignId($customer->getCampaignId());
  10.  
  11.     }
  12.     $this->setCollection($collection);
  13.     return parent::_prepareCollection();
  14. }
  15.        
  16. protected function _prepareCollection() {
  17.  
  18.     $collection = Mage::getResourceModel($this->_getCollectionClass());
  19.     $class = get_class($collection);
  20.     $attribute = Mage::getModel('eav/config')
  21.                 ->getAttribute('customer', 'campaign_id');
  22.     $attributeId = $attribute->getAttributeId();
  23.     $backendType = $attribute->getBackendType();  //probably varchar
  24.  
  25.     $tableName = Mage::getSingleton('core/resource')
  26.             ->getTableName('customer_entity_' . $backendType);
  27.  
  28.     $collection->getSelect()
  29.                 ->joinLeft(array('v' => $tableName),
  30.                         'main_table.customer_id = v.entity_id AND attribute_id = 153',
  31.                         array('v.value', 'v.attribute_id'));
  32.  
  33.     $this->setCollection($collection);
  34.  
  35.     return parent::_prepareCollection();
  36.   }