gpt4 book ai didi

observer-pattern - Magento AddAttributeToSelect() 关于自定义添加的属性(观察者)

转载 作者:行者123 更新时间:2023-12-04 08:28:47 26 4
gpt4 key购买 nike

我在 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;
}
}

如您所见,我正在过滤 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
)
)
)
);
}
}

此属性存储添加产品的管理员。但是,集合并没有根据admin_id进行过滤,在observer方法的foreach()循环中,返回的是NULL,而不是实际的admin_id 它应该返回。

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

最佳答案

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

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

关于observer-pattern - Magento AddAttributeToSelect() 关于自定义添加的属性(观察者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8564159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com