我有一千種產品,希望找到沒有圖像的所有產品。我試圖在管理產品網格中搜索(無圖像),但沒有結果。我怎樣才能做一個SQL查詢來禁用所有這些產品?如何在Magento中找到沒有圖像的所有產品?
回答
我寫了一篇博客文章,然後用sql查詢找到丟失的圖像。它不會禁用產品,但至少是一個開始:http://prattski.com/2010/06/29/magento-sql-to-find-missing-images/。從這一點開始應該很容易做到。如果您的配置不符合我的要求,您可能需要更改屬性ID。
停止思考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
}
如果您想要所有帶有圖像的產品,該怎麼辦? – swl1020 2013-02-26 20:12:21
@ swl1020我還沒有測試過這個,但是一個標準的不等於過濾器('neq')應該可以工作。 '' - > addAttributeToFilter('image',array(「neq」=>'no_selection'));'本文中提供了完整的過濾選項列表:http://alanstorm.com/magento_collections – 2013-02-27 01:14:42
集合遠勝於調試一週中任何一天處理EAV所需的自我強化SQL語句。這項工作已經爲您完成,您只需要注意您使用的邏輯來處理結果。 – 2013-03-09 20:16:13
也得到了查詢艾倫描述了幕後運行的SQL:
echo (string) $products->getSelect();
有兩種方法可以做到:
$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'));
祝你好運!
我知道這是超舊的,但我發現它很有幫助,所以我想我會發布更新。
作爲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' => '%/%/%'
),
));
對於沒有小圖像產品試圖在eav_attribute表這個
select * from catalog_product_entity_varchar WHERE attribute_id = 86 AND value = 'no_selection'
發現attribute_id
我只想補充一點的答案肖恩米肖是正確的,除了我們不需要使用那個
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
),
));
我以各種組合嘗試了所有這些答案,只返回了我的目錄的一小部分。原因:我最初使用定製的產品圖像導入腳本導入我的產品。
如果我在導入期間沒有爲某些行指定圖像,那麼腳本不會爲這些圖像創建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_image
和thumbnail
屬性,但這些屬性可能因安裝而異。在Magento中,使用ORM查詢要比使用SQL好得多。
我嘗試了所有,但是當平板目錄是使
$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只是看哪個產品不具備圖片此爲我的作品:
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
祝您好運!
- 1. Magento - 如何隱藏沒有圖像的產品
- 2. 導出magento中沒有圖像的產品
- 3. Magento:顯示產品頁面上的所有圖像(view.phtml)
- 4. 獲取產品的所有Magento圖像(原始尺寸)
- 5. Magento:防止沒有圖像顯示的產品
- 6. 如何僅使用mysql在magento中拉出所有產品ID,skus,產品名稱,描述,圖像,類別?
- 7. 如何從Magento的1.4.2所有產品數據導入到1.9.0
- 8. Magento Pager沒有顯示在產品list.phtml
- 9. 如何獲得隨機產品,如果我沒有在magento的相關產品
- 10. Magento的1.4 - 顯示所有產品
- 11. 刪除Magento上的所有產品
- 12. 給Magento的所有產品打折
- 13. Magento - 顯示所有產品的總價
- 14. 在magento產品頁面中沒有得到正確的輸出
- 15. 如何在Magento的圖像產品上重疊2張圖像?
- 16. magento產品圖像樣本
- 17. Magento簡單產品圖像
- 18. 我如何獲得所有Magento產品品牌
- 19. 如何使用Magento中沒有websiteID的產品進行產品收藏?
- 20. Magento PHP:沒有父母的產品
- 21. Magento - 沒有註冊的產品提示
- 22. Magento的1.7產品圖像導入未分配圖像產品
- 23. 如何刪除Magento中的所有目錄產品
- 24. 如何刪除/清除magento中的所有產品評論?
- 25. 如何將附加圖像添加到Magento中的產品
- 26. Magento找到孤兒產品
- 27. Magento - 獲取所有產品的產品集合
- 28. Magento - 獲取分組產品的所有關聯產品
- 29. Magento - 導入所有產品/進口產品庫存的區別?
- 30. 刪除所有產品屬性magento
@Michael Myers:你爲什麼在發佈後幾乎整整一年編輯它? – 2011-07-15 17:46:48
@ Zyychin:對編輯沒有任何限制。任何時候你看到一些可以改進的東西,請隨時繼續並做到這一點。在這種情況下,我在這裏是因爲有人發佈了一個我未刪除的答案。 – 2011-07-15 17:53:31
哦,是的,這很公平。 我只看到:由...編輯,並且從放置的方式來看,似乎你顯然編輯了這個問題,而沒有刪除(錯誤)答案! 這樣做更有意義。我向這個社區致敬。 – 2011-07-15 17:58:58