【问题标题】:How to add OR condition in Magento collection for created_at and updated_at date?如何在 Magento 集合中为 created_at 和 updated_at 日期添加 OR 条件?
【发布时间】:2014-02-17 21:32:36
【问题描述】:

我有以下代码

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('category_ids')
            ->addAttributeToSelect('manufacturer')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('special_price')
            ->addAttributeToSelect('regularprice')          
            ->addAttributeToSelect('tier_price_for_bundle') 
            ->addAttributeToSelect('special_from_date') 
            ->addAttributeToSelect('special_to_date')   
            ->addAttributeToFilter('type_id', array('in' => array('simple')))
            ->addAttributeToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));           
            ->addAttributeToFilter('created_at', array('from'=>date("Y-m-d", time()-86400)));

我想在下面为 updated_atcreated_at 添加 OR 条件

->addAttributeToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));           
->addAttributeToFilter('created_at', array('from'=>date("Y-m-d", time()-86400)));

因此,无论产品是创建还是更新,我的收藏都应该加载。

我该怎么做?

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    为 EAV 集合上的 ONE 属性添加 OR 过滤器:属性代码是第一个参数,条件是第二个参数。

    $col->addAttributeToFilter(‘name’, array(array(‘like’ => ‘M%’), array(‘like’ => ‘%O’))); (get items whose name starts with M OR ends with O)
    

    为 EAV 集合上的不同属性添加 OR 过滤器:仅传递一个参数,即具有属性的条件数组。

    $col->addAttributeToFilter(array(array(‘attribute’ => ‘weight’, ‘in’ => array(1,3)), array(‘attribute’ => ‘name’, ‘like’ => ‘M%’)));

    这将获取所有重量为 1 或 3 或名称以 M 开头的项目。

    【讨论】:

      【解决方案2】:

      在集合中添加或条件

         $arrtibuteToFilter =  array(array(
                              'attribute' => $attributeCode,
                              'eq' =>  $attributeValue,
                               ),
              array('attribute' => $attributeCode,
                    'eq' =>  $attributeValue,
                     ));
      
      ->addAttributeToFilter($arrtibuteToFilter);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-23
        • 2011-07-15
        • 2012-09-16
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多