Magento 向集合添加属性的更好方法
我正在重写Mage/Adminhtml/Sales/Order/Grid.php并向prepareCollection添加一些数据。这就是我如何将customer EAV属性campaign_id包含在集合中的方法,但它有点粗糙。我想知道是否有更好的办法Magento 向集合添加属性的更好方法,magento,Magento,我正在重写Mage/Adminhtml/Sales/Order/Grid.php并向prepareCollection添加一些数据。这就是我如何将customer EAV属性campaign_id包含在集合中的方法,但它有点粗糙。我想知道是否有更好的办法 protected function _prepareCollection() { $collection = Mage::getResourceModel($this->_getCollectionClass()); f
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();
}
在加载订单集合之前,需要将客户记录中的数据加入到订单集合中
您可以在加载事件前后观察集合。对于
sales/order\u grid\u collection
collection,这些事件是sales\u order\u grid\u collection\u load\u before
和sales\u order\u grid\u collection\u load\u after
-您将要使用前者。在加载之前,可以通过$observer->getOrderGridCollection()
在您的\u中访问该集合,然后再通过$observer->getOrderGridCollection()加载该数据?你是否也要添加一个要显示的列?我也希望如此。。如果你有更好的解决方案,我很乐意看到。我在上面发布了一个更灵活的解决方案作为答案。在
事件之前,在sales\u order\u grid\u collection\u load\u上使用一个事件观察者来打开所需的字段。这是Magento集合类中自动触发事件的主要实用程序。
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();
}