2012-08-13 42 views
1

下面的代碼我試圖創建一個產品集合和過濾它,但是,它不給我一個準確的產品數量,我不知道爲什麼。Magento不正確的收集計數與過濾器

$collection = Mage::getModel('catalog/product')->getCollection(); 
$collection->addAttributeToSelect('*') 
     ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left') 
     ->addFieldToFilter(array(
       array('attribute'=>'distributor','in'=>array(intval($distributor))), 

     )) 
     ->addAttributeToFilter('category_id', array(
       array('finset' => strval($cat->getId())), 
     )); 
$count = $collection->getSize(); 

例如,我有與分配器濾波器,其具有約30的產品,但由上述代碼的計顯示20同樣爲另一個具有3級的產品,但該計數帶回2.

一個類別

更新:改爲使用finset代替我自己的問題。看到我的回答如下

回答

1

回答我自己的問題。因爲它是一個多重選擇器,所以用於分配器的finset。

$collection = Mage::getModel('catalog/product')->getCollection(); 

        $dists = array(
         array(
         "finset" => array(intval($distributor)) 
        ), 
        ); 

        $collection->addAttributeToSelect('*') 
        ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left outer') 
        ->addAttributeToFilter("distributor", $dists) 
        ->addAttributeToFilter('category_id', array(
         array('finset' => $cat->getId()), 
        )); 
        $collection->load(); 
        $count = $collection->getSize(); 
1

嘗試從此集合中獲取您的查詢代碼並直接在您的數據庫上運行它。你可以檢查查詢是否正確。

試試這個:

$collection->getSelect()->__toString(); 
+0

顯示的計數與我得到的計數是一致的,是的。我試圖做的是模仿高級搜索帶來的結果。例如/高級/結果/?分配器%5B%5D = 13&category = 19該特定的修復帶來超過100個結果,但上面的過濾器代碼僅顯示90的計數。 – Tyler 2012-08-13 17:21:10

+0

當我得到低於價值,那麼我期望我尋找查詢並測試它是否正確。有一天,我的產品網格不帶所有產品,我發現一個錯誤的加入。我不知道你的問題,但我認爲這是正確的方式。 – Guerra 2012-08-13 17:30:43