本文介绍了加入后,无法按属性qty进行过滤 - 从库存库存中获取产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去,你一直很有帮助,我会继续寻求帮助和学习。

You have been so helpful in the past that I keep coming back searching for help and learning.

这次我试图获得所有数量大于1并且有库存的产品( is_in_stock = 1)

This time I am trying to get all products that have a quantity greater than 1 and that are in stock (is_in_stock = 1)

    $products = Mage::getModel('catalog/product')->getCollection();    
    //$products->addAttributeToSelect('*');        

    //SELECT `e`.*, `stock`.`qty` FROM `catalog_product_entity` AS `e` LEFT JOIN `cataloginventory_stock_item` AS `stock` ON stock.product_id = e.entity_id       
    $products->getSelect()->joinLeft(      
       array('stock'=>'cataloginventory_stock_item'),     
       'stock.product_id = e.entity_id',      
       array('stock.qty', 'stock.is_in_stock')      
     );

这会返回 qty is_in_stock 附加到products表的列。您可以如下测试:

This returns qty and is_in_stock columns attached to the products table. You can test it as follows:

$products->getFirstItem()->getQty();        
$products->getFirstItem()->getIsInStock();        

当我尝试过滤 qty is_in_stock

    $products->addFieldToFilter(array(       
       array('Qty','gt'=>'0'),      
       array('Is_in_stock','eq'=>'1'),        
    ));       

这会返回 - 属性名称无效过滤。我猜这是试图搜索 e.qty ,但找不到它。

This returns - Invalid attribute name never performing filtering. I am guessing it is trying search for e.qty but cannot find it.

所以,我尝试用不同的方式过滤:

So, I tried to filter differently:

$products->getSelect()->where("`qty` > 0");        
$products->getSelect()->where("`is_in_stock` = 1");          

这不是过滤,即使,看看它的sql查询, $ c> var_dump((string)$ products-> getSelect())),并且在phpMyAdmin中运行该查询。

This is not filtering as well even though, if you look at its sql query, (var_dump((string) $products->getSelect())), and run that query in phpMyAdmin, it works.

Alan Storm在中提到,在您尝试访问某个项目之前,不会进行数据库查询集合。所以,我做了$ products-> getFirstItem()调用,但它仍然不执行查询或过滤的另一个字。

Alan Storm in his tutorial mentions that 'The database query will not be made until you attempt to access an item in the Collection'. So, I make the $products->getFirstItem() call but it still not executing the query or filtering in another words.

我做错了什么?

再次感谢您,

Margots

推荐答案

查看后,我了解到 _selectAttributes 实例字段在 Mage_Eav_Model_Entity_Collection_Abstract 类中分配,这就是为什么获取异常。一个解决方案通常是Jonathan Day建议的 - 但是添加 addAttributeToFilter()方法。它将返回错误,因为它不能找到此目录/产品的属性。 ( qty in_invetory 位于 cataloginventory_stock_item )。我发现两个问题的解决方案都需要不同的方向:

After looking under the hood I learned that _selectAttributes instance field was not assigned in Mage_Eav_Model_Entity_Collection_Abstract class and that is why get exception. A solution usually would be what Jonathan Day suggested above - add addAttributeToFilter() method, however. It will return error since it cannot find such attribute for catalog/product. (qty and in_invetory are in cataloginventory_stock_item). I found two solutions to my problem both required going different direction:


  1. 一个涉及追求一种方法来查询Select语句,已经设置为产品收集(见上文),但不知何故,它不是重置与新产品的集合。当我复制phpMyAdmin中的Sql statment,它工作,所以如何从产品集合查询该语句:

  1. One involved pursuing a way to query the Select statement that I had set for product collection(see above) but somehow it was not resetting the collection with new product. WhenI copied that Sql statment in phpMyAdmin, it worked, so how to query that statement from product collection:

 $stmt = $products->getConnection('core_write')->query($products->getSelect()->__toString());      

while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){    
  echo "<br>Product Id ".$rows['entity_id'];    
}    


  • 而不是使用 catalog / / code>实体表我使用平表 - cataloginventory_stock_item 来完成同样的事情。

    $stockItem = new Mage_CatalogInventory_Model_Stock_Item();    
    $stockItems->addQtyFilter('>',0);    
    $stockItems->addFieldToFilter('is_in_stock',array('eq'=>'1'));     
    


  • qty> 0 ,并且有现货。

    这篇关于加入后,无法按属性qty进行过滤 - 从库存库存中获取产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-23 20:22