我以各种组合尝试了所有这些答案,但只返回了我目录的一小部分。原因:我最初使用定制的产品图像导入脚本导入了我的产品。
如果我在导入期间没有为某些行指定图像,则脚本不会为这些图像创建 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,它对引用 image、small_image 和 thumbnail 属性的 attribute_id 整数进行硬编码,但这些可能因安装而异。在 Magento 中,使用 ORM 进行查询比使用 SQL 好得多。