views:

30

answers:

2

Hi,

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

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')      
     );

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();        

The issue begins when I try to filter by qty and is_in_stock.

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

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");          

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 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.

What am I doing wrong? Any ideas how to filter by attributes that are joined to the table?

Thank you again,

Margots

+1  A: 

I would suggest that you try using $products->addAttributeToFilter... instead of $products->addFieldToFilter... - the addField method only works when the field is on the main table that you are querying (in this case catalog_product_entity). Because the inventory fields are in a joined table, you need to use addAttribute.

Hope this helps, JD

Jonathan Day
Thank you for replay, however it didn't work. Here is what i did:$products->addAttributeToFilter('is_in_stock', 1); - or -$products->addAttributeToFilter(array( array('attribute' => 'qty', 'gt' =>'0'), ));This is the error i continue go get:Uncaught exception 'Mage_Eav_Exception' with message 'Invalid attribute name: is_in_stock - or -Uncaught exception 'Mage_Eav_Exception' with message 'Invalid attribute name: qty'Any other idea?
latvian
A: 

After looking under the hood I learnt 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' & 'in_invetory' are in cataloginventory_stock_item). I found two solutions to my problem both required going different direction:

  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. When i 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 "
    Product Id ".$rows['entity_id'];
    }

  2. Instead of using 'catalog/product' entity table i used the flat table - 'cataloginventory_stock_item' to accomplish the same thing

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

Now there is a collection of all products with qty>0 and that are in stock

I hope it helps

cheers, Margots

latvian