2013-06-03 129 views
1

我想通過圖像過濾產品集合不爲null。最簡單的方法應該是here提到的方法,但這不起作用:返回的集合沒有元素。Magento:使用集合獲取沒有圖像的產品

我認爲這裏的問題是從來沒有圖像的產品在product/media_gallery表之間沒有關係。因此,當Magento嘗試使用所有這些條件進行篩選時,返回的集合是空的。它沒有考慮到它可能不是相關表之間的任何類型的關係。

$collection->addAttributeToFilter(array(
     array (
      'attribute' => 'image', 
      'like' => 'no_selection' 
     ), 
     array (
      'attribute' => 'image', // null fields 
      'null' => true 
     ), 
     array (
      'attribute' => 'image', // empty, but not null 
      'eq' => '' 
     ), 
     array (
      'attribute' => 'image', // check for information that doesn't conform to Magento's formatting 
      'nlike' => '%/%/%' 
     ), 
    )); 

我想我應該使用joinLeft條件,但我不知道它應該如何。請任何人都可以幫我解決這個問題?

我發現一個very interesting query應該做的伎倆。但我需要這個應用到我的收藏:

SELECT * 
FROM `catalog_product_entity` AS a 
LEFT JOIN `catalog_product_entity_media_gallery` AS b ON a.entity_id = b.entity_id 
WHERE b.value IS NULL 

感謝

回答

3

要將查詢到你的收藏品,你可以使用

$collection->getSelect() 
    ->joinLeft(
     array('_gallery_table' => $collection->getTable('catalog/product_attribute_media_gallery')), 
     'e.entity_id = _gallery_table.entity_id', 
     array() 
    ) 
    ->where('_gallery_table.value IS NULL'); 
+0

我已經更新添加查詢什麼問題應該看起來像,但我不清楚如何建立它... – PauGNU

+0

相應地更新了我的答案 – blmage

+0

謝謝!這似乎工作!我認爲還有一些細節,但現在我可以得到一個基本上我需要的集合。 – PauGNU

相關問題