【问题标题】:How can I find all products without images in Magento?如何在 Magento 中找到所有没有图片的产品?
【发布时间】:2010-08-25 11:35:24
【问题描述】:

我有几千种产品,想查找所有没有图片的产品。我试图在管理产品网格中搜索(无图像),但没有结果。如何进行禁用所有这些产品的 SQL 查询?

【问题讨论】:

  • @Michael Myers:你为什么在它发布将近整整一年后才编辑它?
  • @Zéychin:编辑没有诉讼时效。每当您看到可以改进的地方时,请随时继续进行。在这种情况下,我来到这里是因为有人发布了我要删除的非答案。
  • 哦,是的,这很公平。我只是看到:由...编辑,从它的放置方式来看,您似乎显然编辑了问题,而不是删除了(错误的)答案!这更有意义。我向你为这个社区所做的工作致敬。

标签: magento


【解决方案1】:

停止思考 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 我没有对此进行测试,但是标准的不相等过滤器(neq)应该可以工作。 ``->addAttributeToFilter('image', array("neq"=>'no_selection'));`本文提供了过滤选项的完整列表:alanstorm.com/magento_collections
  • 集合比调试一周中任何一天使用 EAV 所需的自混淆 SQL 语句要好得多。工作已经为您完成,您只需注意处理结果时使用的逻辑。
  • 谢谢@AlanStorm,你是天才
【解决方案2】:

我知道这是超级旧的,但我发现它很有帮助,所以我想我会发布一个更新。

作为 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' => '%/%/%'
        ),
    ));

【讨论】:

    【解决方案3】:

    我以各种组合尝试了所有这些答案,但只返回了我目录的一小部分。原因:我最初使用定制的产品图像导入脚本导入了我的产品。

    如果我在导入期间没有为某些行指定图像,则脚本不会为这些图像创建 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 查询以从 SQL 客户端导出为 CSV,只需从 PHP 脚本打印查询:

    echo $products->getSelect();
    

    我在 StackOverflow 上看到过一些 SQL,它对引用 imagesmall_imagethumbnail 属性的 attribute_id 整数进行硬编码,但这些可能因安装而异。在 Magento 中,使用 ORM 进行查询比使用 SQL 好得多。

    【讨论】:

      【解决方案4】:

      另外,要获取 Alan 描述的查询在幕后运行的 sql:

      echo (string) $products-&gt;getSelect();

      【讨论】:

        【解决方案5】:

        不久前我写了一篇博客文章,使用 sql 查询来查找丢失的图像。它不会禁用产品,但至少是一个开始:http://prattski.com/2010/06/29/magento-sql-to-find-missing-images/。从这一点开始应该很容易做到。如果您的属性 ID 与我的不匹配,您可能需要更改属性 ID。

        【讨论】:

          【解决方案6】:

          有两种方法:

          $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'));
          

          祝你好运!

          【讨论】:

            【解决方案7】:

            对于没有小图像的产品试试这个

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

            在eav_attribute表中查找attribute_id

            【讨论】:

              【解决方案8】:

              我只想补充一点,Sean Michaud 的答案是对的,只是我们不需要使用它

              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
                      ),
              ));
              

              【讨论】:

                【解决方案9】:

                我尝试了所有方法,但启用平面目录后这对我有用

                $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'
                    );
                

                【讨论】:

                  【解决方案10】:

                  您可以使用此 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
                  

                  祝你好运!

                  【讨论】:

                    猜你喜欢
                    • 2014-04-04
                    • 2023-03-18
                    • 1970-01-01
                    • 2013-12-31
                    • 1970-01-01
                    • 2012-07-14
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-12-31
                    相关资源
                    最近更新 更多