2

我正在拉一个集合如下

$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 OR upc OR jan 不为空的情况下选择所有简单产品。

换句话说,只要产品具有 ean、upc 或 jan,就选择所有产品。如果它们都是空的,请不要选择它。

4

1 回答 1

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'      => '',
        ),
    )
);
于 2012-08-06T15:25:47.030 回答