2010-08-25 74 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 

祝您好运!

相关问题