2012-08-06 109 views
2

我拉着收集如下Magento的收集過濾器,其中的任何屬性爲空

$collection = Mage::getModel('catalog/product')->getCollection() 
->addAttributeToSelect('jan') 
->addAttributeToSelect('name') 
->addAttributeToSelect('upc') 
->addAttributeToSelect('ean') 
->addAttributeToSelect('price') 
->addAttributeToFilter('ean', array('neq' => '')) 
->addFieldToFilter(array(
    array('attribute'=>'type_id','eq'=>'simple')   
)) 
->setPage(1,5); 

目前我選擇的所有簡單的產品,其中「EAN」不爲空。我如何選擇所有簡單的產品在哪裏或是否是空的。

換句話說,只要產品有ean,upc或jan,就選擇所有產品。如果它們都是空的,請不要選擇它。

回答

4

http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

class Mage_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Collection_Abstract 

-

class Mage_Catalog_Model_Resource_Collection_Abstract extends Mage_Eav_Model_Entity_Collection_Abstract 

-

Mage_Eav_Model_Entity_Collection_Abstract 

/** 
* Add attribute filter to collection 
* 
* If $attribute is an array will add OR condition with following format: 
* array(
*  array('attribute'=>'firstname', 'like'=>'test%'), 
*  array('attribute'=>'lastname', 'like'=>'test%'), 
*) 
* 
* @see self::_getConditionSql for $condition 
* @param Mage_Eav_Model_Entity_Attribute_Interface|integer|string|array $attribute 
* @param null|string|array $condition 
* @param string $operator 
* @return Mage_Eav_Model_Entity_Collection_Abstract 
*/ 
public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner') 

解決方案:

$collection->addAttributeToFilter(
array(
    array(
     'attribute' => 'ean', 
     'neq'  => '', 
     ), 
    array(
     'attribute' => 'upc', 
     'neq'  => '', 
     ), 
    ), 
    array(
     'attribute' => 'jan', 
     'neq'  => '', 
     ), 
    ) 
); 
+0

您的解決方案工作,塔NKS! – 2012-08-06 16:16:09