自定义添加属性(观察者)上的Magento AddAttributeToSelect()

Hos*_*diq 3 magento observer-pattern magento-1.5

我已经设置了一个观察者,catalog_product_collection_load_after并调用以下代码:

<?php
class Drench_Admindetails_Model_Observer {
    public function loadAfter($observer){
        $collection = $observer->getEvent()->getCollection();
        $collection->addAttributeToFilter('admin_id', Mage::getSingleton('admin/session')->getUser()->getUserId());
        foreach($collection as $item) {
            fb($item->getAdminId()); //fb() is a firebug call
        }
        return $this;
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在过滤集合admin_id,我通过以下安装脚本(namespace/module/Resource/Eav/Mysql4/Setup.php)创建了集合.

<?php

class Drench_Admindetails_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
    public function getDefaultEntities()
    {
        return array(
            'catalog_product'                        => array (
                'entity_model'                       => 'catalog/product',
                'attribute_model'                    => 'catalog/resource_eav_attribute',
                'table'                              => 'catalog/product',
                'additional_attribute_table'         => 'catalog/eav_attribute',
                'entity_attribute_collection'        => 'catalog/product_attribute_collection',
                'attributes'                         => array (
                    'admin_id'                       => array (
                        'group'                      => '',
                        'label'                      => '',
                        'type'                       => 'int',
                        'input'                      => '',
                        'default'                    => '0',
                        'class'                      => '',
                        'backend'                    => '',
                        'frontend'                   => '',
                        'source'                     => '',
                        'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                        'visible'                    => false,
                        'required'                   => false,
                        'user_defined'               => false,
                        'searchable'                 => false,
                        'filterable'                 => false,
                        'comparable'                 => false,
                        'visible_on_front'           => false,
                        'visible_in_advanced_search' => false,
                        'unique'                     => false
                    )
               )
           )
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

此属性存储添加产品的管理员.但是,集合不会被过滤admin_id,并且在foreach()观察者方法的循环中,它返回NULL而不是admin_id它应返回的实际值.

关于它为什么不起作用的任何想法?

clo*_*eek 5

加载后,您无法过滤集合.请改用事件catalog_product_collection_load_before.如果此时尝试迭代集合,它可能会调用相同的事件并开始无限递归,这将是不好的.

admin_id除非属性used_in_product_listing设置为,否则该属性可能不会添加到产品列表的选定列中true.您也可以成功使用$collection->addAttributeToSelect('admin_id')相同的加载前事件.