代码之家  ›  专栏  ›  技术社区  ›  leepowers

magento addfieldtofilter允许空值

  •  10
  • leepowers  · 技术社区  · 16 年前

    使用magento集合方法addfieldtofilter时,是否可以允许按空值筛选?我要选择集合中具有自定义属性的所有产品,即使没有为该属性指定值。

    5 回复  |  直到 8 年前
        1
  •  2
  •   Alireza Eliaderani    16 年前

    你不需要使用 addFieldToFilter .

    现在的解决方案是:
    属性名称为 code 在magento中,您只需使用下面的代码就可以获得所有具有特定属性的产品作为数组。

    
    $prodsArray=Mage::getModel('catalog/product')->getCollection()
                                      ->addAttributeToFilter('custom_attribute_code')
                                      ->getItems();
    
    

    还可以在中为属性值指定某些条件 addAttributeToFilter 在addattributeToFilter的第二个参数中。

    您可以在此文件中找到此方法(供进一步研究):

    app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
    
        2
  •  53
  •   Kat    16 年前

    我看到你已经找到了一个解决方案,但也有这个选项:

    $collection->addFieldToFilter('parent_item_id', array('null' => true));
    

    但如果要使用“null”=>false,则不起作用。 (我注意到您可以使用“in”、“nin”、“eq”、“neq”、“gt”等元素),您可以这样做:

    $collection->addFieldToFilter('parent_item_id', array('neq' => 'NULL' ));
    

    希望这仍然有帮助…

        3
  •  21
  •   Dane    15 年前

    这适用于非空筛选器

    $collection->addFieldToFilter('parent_item_id', array('notnull' => true));
    
        4
  •  19
  •   leepowers    16 年前

    通过空/空属性筛选产品集合有两个可能的解决方案。Magento使用内部联接来获取要筛选的属性值。但如果没有为product属性分配值,则联接将失败,因为缺少数据库表/关系。

    解决方案1: 使用addattributeToFilter()并将连接类型从“inner”(默认)更改为“left”:

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToFilter('custom_attribute', array( ... condition options ..), 'left');
    

    解决方案2: 确保自定义属性具有默认值。马根托在这方面是保守的。如果为属性指定了值,Magento将只创建属性和产品之间的关系。因此,如果缺少用户指定的值或默认值,则无法访问该属性来筛选产品。 即使该属性出现在“管理面板”下的“产品详细信息”视图中。

        5
  •  2
  •   8qfx1ai5    8 年前

    因为问题与问题的标题不完全匹配,我通过搜索特殊值或空等条件多次找到了它们

    如果要筛选与值或空值匹配的集合,则可以使用:

    $collection->addFieldToFilter('<attribute>', array(
      array('eq' => '<value>'),
      array('null' => true)
    ));
    
    推荐文章