题
我正在做以下操作:
$productCollection = Mage::getModel('catalog/product')
->getCollection();
$productCollection
->addAttributeToFilter('my_attribute', 1);
my_attribute
不在平坦的桌子中,而是启用了扁平桌子。
我不断获得完整的收藏。
原因似乎在 \Mage_Catalog_Model_Resource_Product_Collection::addAttributeToSelect
:
$columns = $this->getEntity()->getAttributeForSelect($attributeCode);
不 $this->getEntity()
是一个实例 Mage_Catalog_Model_Resource_Product_Flat
它可以获取平面字段 - 如果找不到,只需返回null即可。
什么是在收集过滤器中添加非灯泡属性的干净方法?
就我而言,将属性添加到平桌上没有感觉。
解决方案
您可以自己加入必要的桌子。
$productCollection = Mage::getModel('catalog/product')
->getCollection();
$table = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'my_attribute')->getBackend()->getTable();
$attributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'my_attribute')->getAttributeId();
$productCollection->getSelect()->join(array('attributeTable' => $table), 'e.entity_id = attributeTable.entity_id', array('my_attribute' => 'attributeTable.value'))
->where("attributeTable.attribute_id = ?", $attributeId)
->where("attributeTable.value = ?", 1);
您可能也想通过Store_id加入。
其他提示
黑客(CE 1.6.2.0+)将通过阵列传递该条件,并且不相信它的作用:
$collection->addFieldToFilter(array(array('attribute' => 'my_attribute', 'eq' => 1)));
Colinm的答案工作的原因是由于代码 app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php
' addAttributeToFilter
方法。如果您使用此数组格式,则不会调用 addAttributeToSelect
. 。在平坦模式下, addAttributeToSelect
如果属性不在平桌上,则会默默失败。
(下面是我的回答 https://stackoverflow.com/questions/6271284/can-i-add-other-atributes-to-magentos-flat-product-catalog-table/17021620 - 我不确定这是什么礼节,但知道我会发现它有帮助)
我想要一个“干净”解决方案,用于在非燃料属性上选择和过滤的扁平模式集合,该解决方案:
- 不需要属性在管理员中具有特定的设置(用户可能会添加它,也可以隐藏在前端)
- 适用于平坦和非平板模式
我使用了关联的产品集合,但这适用于任何EAV集合。
失败代码:
$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
->addAttributeToSelect( 'my_custom_attribute' )
->addAttributeToFilter( 'my_custom_attribute', 3 )
;
在平坦模式下,上面的代码如果不在平桌上,则无法在属性上选择或过滤。
添加到选择:
$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
->joinAttribute( 'my_custom_attribute', 'catalog_product/my_custom_attribute', 'entity_id', null, 'left' )
->addAttributeToSelect( 'my_custom_attribute' )
;
这 joinAttribute
方法将所请求的特定属性的查询加入。当属性已经在平桌上时,它仍然可以工作,但是效率略低于纯使用平桌。
我用过了 left
加入那里,以确保它可以获取产品 my_custom_attribute
没有设置这些产品。更改它 inner
如果您只对行感兴趣 my_custom_attribute
设置。
添加到过滤器(按照上面的colinm):
$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
->addAttributeToFilter( array( array( 'attribute' => 'my_custom_attribute', 'eq' => 3 ) ) )
;
以上代码将其添加到选择中以及遵守过滤器。
(在CE 1.6.2.0中测试)
在里面 Mage_Rss
他们使用A Hacky-Method禁用平坦桌子。他们使用这样的事实,即扁平桌子总是在管理员商店中放下,因此只需模仿管理员商店即可。
class Mage_Rss_Helper_Data {
[...]
/**
* Disable using of flat catalog and/or product model to prevent limiting results to single store. Probably won't
* work inside a controller.
*
* @return null
*/
public function disableFlat()
{
/* @var $flatHelper Mage_Catalog_Helper_Product_Flat */
$flatHelper = Mage::helper('catalog/product_flat');
if ($flatHelper->isEnabled()) {
/* @var $emulationModel Mage_Core_Model_App_Emulation */
$emulationModel = Mage::getModel('core/app_emulation');
// Emulate admin environment to disable using flat model - otherwise we won't get global stats
// for all stores
$emulationModel->startEnvironmentEmulation(0, Mage_Core_Model_App_Area::AREA_ADMINHTML);
}
}
启动仿真后,您应该将其重置 emulationModel->stopEnvironmentEmulation()
当您创建属性时,它应该在全局级别上进行过滤。这样,它将在Layared Navigation中使用。此外,它将要求该属性为下拉列表或多选择。在这种情况下,我个人会建议不要更改核心文件以满足您的需求