2014-10-22 33 views
1

我知道我在Magento目錄中有一些產品完全缺少websiteID。我試圖找到這樣的產品:如何使用Magento中沒有websiteID的產品進行產品收藏?

$products = Mage::getModel('catalog/product') 
    ->getCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('website_ids', 'null'); 

然而這會崩潰。提取沒有設置website_id的產品收集的正確方法是什麼?

回答

1

所有的產品網站id存儲在catalog_product_website。如果您在此處打開此表格,則可以看到與網站相關的產品。

這裏是過濾器,

<?php 

require_once('app/Mage.php'); //Path to Magento 
umask(0); 
Mage::app(); 

echo '<pre>'; 
$collection = Mage::getResourceModel('catalog/product_collection'); 
$collection->addWebsiteFilter('0'); 

//echo $collection->getSelect(); exit; 
$collection->getFirstItem(); 
// ->addAttributeToFilter('website_ids', 'null'); 

//echo $orders->getSelect(); 

//exit; 

print_r($collection); 

在這種$collection->addWebsiteFilter('0');過濾webssite IDS(當前爲0),這樣你就可以更改值as1,0,2或NULL。而已。

在你的情況下,嘗試使用NULL或空像''。

更新:

如果用''過濾器,你會得到錯誤,因爲有與產品實體ID和網站ID,這意味着該website_id不能爲空外鍵的表catalog_product_website。所以請嘗試直接查詢。 Magento中,你可以通過這個得到集合查詢,

echo $collection->getSelect(); exit; 

或使用,

SELECT `e`.* FROM `catalog_product_entity` AS `e` 
INNER JOIN `catalog_product_website` AS `product_website` ON product_website.product_id = e.entity_id AND product_website.website_id IN('') 

如果您有任何疑問,請在這裏評論。

+0

感謝您的幫助,但是使用「」或「空」返回「無效的網站代碼請求」,並使用NULL返回所有產品。也許我必須通過對錶格的直接查詢來做到這一點。 :/ – DarkVader 2014-10-23 08:20:48

1

這爲我工作

SELECT catalog_product_entity.entity_id 
FROM catalog_product_entity 
LEFT JOIN catalog_product_website 
    ON catalog_product_entity.entity_id = catalog_product_website.product_id 
WHERE catalog_product_website.product_id IS NULL