• magento addFieldToFilter()方法常用的过滤条件


    记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件。以下参数也同样适用于产品实体的addAttributeToFilter()方法。
     

    //等于 Equals: eq
    $_products->addAttributeToFilter('status', array('eq' => 1));
     
    //不等于 Not Equals - neq
    $_products->addAttributeToFilter('sku', array('neq' => 'test-product'));
     
    //Like - like
    $_products->addAttributeToFilter('sku', array('like' => 'UX%'));
     
    //Not Like - nlike
    $_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
     
    //In - in
    $_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
     
    //Not In - nin
    $_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
     
    //NULL - null
    $_products->addAttributeToFilter('description', 'null');
     
    //Not NULL - notnull
    $_products->addAttributeToFilter('description', 'notnull');
     
    //大于 Greater Than - gt
    $_products->addAttributeToFilter('id', array('gt' => 5));
     
    //小于 Less Than - lt
    $_products->addAttributeToFilter('id', array('lt' => 5));
     
    //大于等于 Greater Than or Equals To- gteq
    $_products->addAttributeToFilter('id', array('gteq' => 5));
     
    //小于等于 Less Than or Equals To - lteq
    $_products->addAttributeToFilter('id', array('lteq' => 5));
     

    也可以有:
    $collection->addAttributeToFilter(

        array(
            array('attribute'=> 'someattribute','like' => 'value'),
            array('attribute'=> 'otherattribute','like' => 'value'),
            array('attribute'=> 'anotherattribute','like' => 'value'),
        )
    );
     

    实例:
     

    $collection->addFieldToFilter('max_item_count',
                    array(
                        array('gteq' => 10),
                        array('null' => true),
                    )
            )
            ->addFieldToFilter('max_item_price',
                    array(
                        array('gteq' => 9.99),
                        array('null' => true),
                    )
            )
            ->addFieldToFilter('max_item_weight',
                    array(
                        array('gteq' => 1.5),
                        array('null' => true),
                    )
            );
    
    

    Which results in this SQL:

     

    SELECT `main_table`.*
    FROM `shipping_method_entity` AS `main_table`
    WHERE (((max_item_count >= 10) OR (max_item_count IS NULL)))
      AND (((max_item_price >= 9.99) OR (max_item_price IS NULL)))
      AND (((max_item_weight >= 1.5) OR (max_item_weight IS NULL)))
     

    addFieldToFilter()
    据我所知,addAttributeToFilter只适用于在Magento产品。当我第一次发现了这个事实,我不仅感到震惊,我很担心!我想,没有 它,我将有我所有的SQL查询定制工艺。找Magento的核心代码后,我发现addFieldToFilter()。这个功能,在相同的方式工作,并采 用相同的参数研究,但它适用于所有集合,不只是产品!

    关于addFieldToFilter()的一些用法请看:http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

    调试SQL查询
    有两种方法调试加载在Magento集合时正在执行的查询。
     

    // Method 1
    Mage::getModel('catalog/product')->getCollection()->load(true);
    
    // Method 2 (Quicker, Recommended)
    $collection = Mage::getModel('catalog/product')->getCollection();
    echo $collection->getSelect();
     

    方法1和方法2都是打印查询的,但各自有略微不同的方式。方法1,打印查询以及装载产品,而方法2将只查询对象转换为一个字符串(即会打印出的SQL语句)。第二种方法是肯定更好,更快,因为它会被执行,但我有参考他们都在这里。

    在一个侧面说明,我将很快被写上getSelect()函数的文章,因为它开辟了一个门在Magento集合,让他们(和你)真正的力量!

    Magento的集合实际上就是一个包含其它模型的模型,所以我们可以用Product集合来取代数组存放一组产品。集合除了可以提供一个更为便捷的模型分组数据结构,还提示一些可用于操作实体集合的一些特定方法,其中较为有用的方法有:

    • addAttributeToSelect:用于为集合中实体添加属性,可使用星号*来作为通配符来添加所有属性
    • addFieldToFilter:用于为集合添加一个属性滤镜,该函数用于普通的非EAV模型
    • addAttributeToFilter:方法用于过滤EAV实体中的集合
    • addAttributeToSort:该方法用于添加属性来进行排序
    • addStoreFilter:该方法用于存储可用性滤镜,包含可用产品
    • addWebsiteFilter:该方法为集合添加一个站点滤镜
    • addCategoryFilter:该方法用于为产品集合指定分类滤镜
    • addUrlRewrite:该方法用于向产品添加URL重写数据
    • setOrder:该方法用于设定集合的排序

    这里仅列出了一部分集合方法,每个集合依据相应的实体类型采用不同的方法。例如,customer集合 Mage_Customer_Model_Resource_Customer_Collection对应一个特定方法groupByEmail(),从 名称就可以看出它是一个对内部实体email的分组。
    参照前面的例子,我们还是会使用产品模型,就当前而言产品集合:

    Magento模型集合addFieldToFilter常用过滤条件

    Magento的addFieldToFilter方法支持如下条件表达式:

    Attribute code SQL condition
    eq =
    neq !=
    like LIKE
    nlike NOT LIKE
    in IN ()
    nin NOT IN ()
    is IS
    notnull NOT NULL
    null NULL
    moreq >=
    gt >
    lt <
    gteq >=
    lteq <=

    我们还可以使用其它类型的过滤,比如在上面的日期过滤代码后添加如下代码来获取可见产品:


    $productCollection->addAttributeToFilter('visibility', 4);

    这个可见性属性是由产品用来控制产品在何处显示的特殊属性,它可以有如下值:

    • 单独不可见:值为1
    • 目录中可见:值为2
    • 搜索中可见:值为3
    • 目录和搜索中可见:值为4
  • 相关阅读:
    lua的多种实现方式(1-100的和)
    51单片机交通灯(定时器+38译码器+中断)
    51单片机定时器实现LED闪烁
    51单片机0号与1号外部中断实例
    51单片机:IO口扩展芯片用法(74HC165,74HC595)
    mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    Intellij IDEA运行报Command line is too long解法
    Jmeter命令行运行实例讲解
    Windows10在当前目录快速打开cmd的方法
    Jmeter接口测试对json串中的值进行断言
  • 原文地址:https://www.cnblogs.com/zhengyanbin2016/p/5606917.html
Copyright © 2020-2023  润新知