2012-10-23 27 views
2

我是Magento getResourceModel的新手,我試圖添加一個簡單的過濾器到我的查詢,但我無法用getResourceModel。Magento:減法和分區上addAttributeToFilter

原始查詢:

$collection = Mage::getResourceModel('catalog/product_collection'); 
Mage::getModel('catalog/layer')->prepareProductCollection($collection); 
$collection->addAttributeToFilter('promotion', 1)->setOrder('price', 'desc'); 

我只是想添加where子句:

(`price` - `final_price`) >= (`price` * 0.4) 

有人能幫助我做到這一點?

這就是全部,謝謝!

+0

也許這有點幫助:http://blog.onlinebizsoft.com/addattributetofilter-conditionals-in-magento/ – feeela

+0

感謝費拉,我曾看到過一篇博客文章,並且我訪問了官方的magento文檔。但我找不到如何進行分割和減法等簡單操作。 –

+0

我不知道是否可以使用Varian_Data_Collection_Db(它是Magento中任何集合的基類),但我猜你需要手動修改SELECT。 '$ collection-> getSelect()'應該返回'Varien_Db_Select'的一個實例 - 在'/ lib/Varien/Db/Select.php'中查看該類。 – feeela

回答

2

因此,最後我找到了正確的方法來做到這一點,抱歉,延遲發佈答案在這裏,謝謝@feeela。

展望文件/lib/Zend/Db/Select.php我發現,存在於其中功能:

public function where($cond, $value = null, $type = null) 
{ 
    $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, true); 

    return $this; 
} 

所以,我們需要的就是添加一個調用這個函數給了我們需要的條件。就我而言,我只是添加一個條件來過濾有40%折扣的產品。

$collection = Mage::getResourceModel('catalog/product_collection'); 
Mage::getModel('catalog/layer')->prepareProductCollection($collection); 
$collection->addAttributeToFilter('promotion', 1) 
      ->addStoreFilter(); 
$collection->getSelect()->where('(`price` - `final_price`) >= (`price` * 0.4)'); 

所以,我希望這可以幫助一些帥哥!

Grazie tutti!

相關問題