2010-08-25 79 views
7

我有一千種產品,希望找到沒有圖像的所有產品。我試圖在管理產品網格中搜索(無圖像),但沒有結果。我怎樣才能做一個SQL查詢來禁用所有這些產品?如何在Magento中找到沒有圖像的所有產品?

+0

@Michael Myers:你爲什麼在發佈後幾乎整整一年編輯它? – 2011-07-15 17:46:48

+0

@ Zyychin:對編輯沒有任何限制。任何時候你看到一些可以改進的東西,請隨時繼續並做到這一點。在這種情況下,我在這裏是因爲有人發佈了一個我未刪除的答案。 – 2011-07-15 17:53:31

+1

哦,是的,這很公平。 我只看到:由...編輯,並且從放置的方式來看,似乎你顯然編輯了這個問題,而沒有刪除(錯誤)答案! 這樣做更有意義。我向這個社區致敬。 – 2011-07-15 17:58:58

回答

15

停止思考SQL。從Magento的模型開始思考。 Magento的模型恰好使用SQL作爲後端。通過原始SQL查詢事情是可能的,但是會根據Magento的版本而有所不同,並且可能會因您使用的後端而有所不同。

從測試控制器操作或其他可以執行Magento代碼的地方運行以下命令。它查詢產品型號無圖像

//this builds a collection that's analagous to 
//select * from products where image = 'no_selection' 
$products = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToFilter('image', 'no_selection'); 

foreach($products as $product) 
{ 
    echo $product->getSku() . " has no image \n<br />\n"; 
    //var_dump($product->getData()); //uncomment to see all product attributes 
            //remove ->addAttributeToFilter('image', 'no_selection'); 
            //from above to see all images and get an idea of 
            //the things you may query for 
}  
+0

如果您想要所有帶有圖像的產品,該怎麼辦? – swl1020 2013-02-26 20:12:21

+0

@ swl1020我還沒有測試過這個,但是一個標準的不等於過濾器('neq')應該可以工作。 '' - > addAttributeToFilter('image',array(「neq」=>'no_selection'));'本文中提供了完整的過濾選項列表:http://alanstorm.com/magento_collections – 2013-02-27 01:14:42

+0

集合遠勝於調試一週中任何一天處理EAV所需的自我強化SQL語句。這項工作已經爲您完成,您只需要注意您使用的邏輯來處理結果。 – 2013-03-09 20:16:13

1

也得到了查詢艾倫描述了幕後運行的SQL:

echo (string) $products->getSelect();

0

有兩種方法可以做到:

$products = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToFilter('small_image',array('notnull'=>'','neq'=>'no_selection')); 

上面的代碼應該工作,但在我的情況下,它不工作。所以我嘗試以下:

$products = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToFilter('small_image',array('neq'=>'no_selection')); 

祝你好運!

6

我知道這是超舊的,但我發現它很有幫助,所以我想我會發布更新。

作爲Alan上述答案的補充,我發現除了'no_selection'之外,還有其他場景可能是由於插件或系統中的一般錯誤?最後的nlike會真的找到所有的東西,但我只是爲了好玩而留下其他人。

改變集合查詢,如下所示:

$products = Mage::getModel('catalog/product') 
    ->getCollection() 
    ->addAttributeToSelect('*') 
    ->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' => '%/%/%' 
     ), 
    )); 
0

對於沒有小圖像產品試圖在eav_attribute表這個

select * from catalog_product_entity_varchar WHERE attribute_id = 86 AND value = 'no_selection' 

發現attribute_id

0

我只想補充一點的答案肖恩米肖是正確的,除了我們不需要使用那個

array (
     'attribute' => 'image', // null fields 
     'null' => true 
    ), 

使用該網頁:http://bytes.com/topic/sql-server/answers/83267-wildcard-doesnt-match-using-like-varchar-field-wierd

「NULL值是一樣零長度的字符串。NULL 表示沒有任何價值的,而SQL標準說,一個 NULL值永遠不會與任何其他價值,包括另一個NULL」

所以%/%/%不會得到NULL值,但添加的代碼從上面我們會修正這個錯誤,並得到與NULL值圖像領域。這是結果

$products = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToFilter(array(
    array (
     'attribute' => 'image', // null fields 
     'null' => true 
    ), 
    array (
     'attribute' => 'image', // check for information that doesn't conform to Magento's formatting 
     'nlike' => '%/%/%' 
    ), 
)); 

如果你想工作的所有圖像屬性,代碼可能是這樣的

$products = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToFilter(array(
     array (
      'attribute' => 'image', //Check for information that doesn't conform to Magento's formatting 
      'nlike' => '%/%/%' 
     ), 
     array (
      'attribute' => 'small_image', //Check for information that doesn't conform to Magento's formatting 
      'nlike' => '%/%/%' 
     ), 
     array (
      'attribute' => 'thumbnail', //Check for information that doesn't conform to Magento's formatting 
      'nlike' => '%/%/%' 
     ), 
     array (
      'attribute' => 'image', //Check for null fields 
      'null' => true 
     ), 
     array (
      'attribute' => 'small_image', //Check for null fields 
      'null' => true 
     ), 
     array (
      'attribute' => 'thumbnail', //Check for null fields 
      'null' => true 
     ), 
)); 
2

我以各種組合嘗試了所有這些答案,只返回了我的目錄的一小部分。原因:我最初使用定製的產品圖像導入腳本導入我的產品。

如果我在導入期間沒有爲某些行指定圖像,那麼腳本不會爲這些圖像創建NULL或空屬性值。它根本沒有創建屬性行。

由於addAttributeToFilter默認使用INNER連接,並且沒有要加入的圖像屬性值,所以此處發佈的查詢未收到這些SKU。

以下代碼返回該圖像,small_image 縮略圖爲空的所有產品,格式不正確,或該行被完全丟失

addAttributeToFilter的第三個參數允許您指定要與WHERE語句的OR子句一起使用的連接類型。

$products = Mage::getModel('catalog/product') 
    ->getCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter(
     array(
      array(
       'attribute' => 'image', 
       'null' => '1' 
      ), 
      array(
       'attribute' => 'small_image', 
       'null' => '1' 
      ), 
      array(
       'attribute' => 'thumbnail', 
       'null' => '1' 
      ), 
      array(
       'attribute' => 'image', 
       'nlike' => '%/%/%' 
      ), 
      array(
       'attribute' => 'small_image', 
       'nlike' => '%/%/%' 
      ), 
      array(
       'attribute' => 'thumbnail', 
       'nlike' => '%/%/%' 
      ) 
     ), 
     null, 
     'left' 
    ); 

如果像我一樣,你想將其轉換爲一個SQL查詢來爲CSV從SQL客戶端出口,只需打印從PHP腳本查詢:

echo $products->getSelect(); 

我已經看到StackOverflow上發佈了一些SQL,它硬編碼了attribute_id整數,這些整數涉及image,small_imagethumbnail屬性,但這些屬性可能因安裝而異。在Magento中,使用ORM查詢要比使用SQL好得多。

0

我嘗試了所有,但是當平板目錄是使

$products = Mage::getModel('catalog/product') 
    ->getCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter(
     array(
      array(
       'attribute' => 'image', 
       'null' => '1' 
      ), 
      array(
       'attribute' => 'small_image', 
       'null' => '1' 
      ), 
      array(
       'attribute' => 'thumbnail', 
       'null' => '1' 
      ), 
      array(
       'attribute' => 'image', 
       'nlike' => '%/%/%' 
      ), 
      array(
       'attribute' => 'small_image', 
       'nlike' => '%/%/%' 
      ), 
      array(
       'attribute' => 'thumbnail', 
       'nlike' => '%/%/%' 
      ) 
     ), 
     null, 
     'left' 
    ); 
0

您可以使用此SQL只是看哪個產品不具備圖片此爲我的作品:

SELECT * FROM catalog_product_entity_media_gallery RIGHT OUTER JOIN catalog_product_entity ON catalog_product_entity.entity_id = catalog_product_entity_media_gallery.entity_id WHERE catalog_product_entity_media_gallery.value is NULL 

祝您好運!

相關問題