将属性添加到集合的更好方法

时间:2012-01-11 18:04:43

标签: magento

我正在覆盖Mage / Adminhtml / Sales / Order / Grid.php并向prepareCollection添加一些数据。这就是我将客户EAV属性campaign_id包含在集合中的方式,但它有点像hacky。我想知道是否有更好的方法。

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    foreach ($collection as &$object){
        $customer = Mage::getModel('customer/customer')
            ->setId($object->getCustomerId())
            ->load();
        $object->setCampaignId($customer->getCampaignId());

    }
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

2 个答案:

答案 0 :(得分:8)

您需要在加载订单集合之前将客户记录中的数据加入到订单集合中。

你可以在之前和之前观察收藏品。加载事件后。对于sales/order_grid_collection收集,这些活动为sales_order_grid_collection_load_beforesales_order_grid_collection_load_after - 您需要使用前者。您可以通过_before_load$observer->getOrderGridCollection()事件观察员中访问该集合。

答案 1 :(得分:0)

  protected function _prepareCollection() {

    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $class = get_class($collection);
    $attribute = Mage::getModel('eav/config')
                ->getAttribute('customer', 'campaign_id');
    $attributeId = $attribute->getAttributeId();
    $backendType = $attribute->getBackendType();  //probably varchar

    $tableName = Mage::getSingleton('core/resource')
            ->getTableName('customer_entity_' . $backendType);

    $collection->getSelect()
                ->joinLeft(array('v' => $tableName),
                        'main_table.customer_id = v.entity_id AND attribute_id = 153',
                        array('v.value', 'v.attribute_id'));

    $this->setCollection($collection);

    return parent::_prepareCollection();
  }