【问题标题】:Magento: Get products without images using collectionsMagento:使用集合获取没有图像的产品
【发布时间】:2013-06-03 08:16:21
【问题描述】:

我想按不为空的图像过滤产品集合。最简单的方法应该是here提到的方法,但这不起作用:返回的集合没有元素。

我认为这里的问题是,从未有图像的产品在 product/media_gallery 表之间没有关系。因此,当 Magento 尝试使用所有这些条件进行过滤时,返回的集合是空的。它没有考虑到所涉及的表之间可能不是任何类型的关系。

$collection->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' => '%/%/%'
        ),
    ));

我想我应该使用 joinLeft 条件,但我不知道应该怎么做。有人可以帮我解决这个问题吗?

我找到了一个very interesting query 应该可以解决问题。但我需要将此应用到我的收藏中:

SELECT *
FROM `catalog_product_entity` AS a
LEFT JOIN `catalog_product_entity_media_gallery` AS b ON a.entity_id = b.entity_id
WHERE b.value IS NULL

谢谢

【问题讨论】:

    标签: magento


    【解决方案1】:

    要将查询应用于您的产品集合,您可以使用

    $collection->getSelect()
        ->joinLeft(
            array('_gallery_table' => $collection->getTable('catalog/product_attribute_media_gallery')),
            'e.entity_id = _gallery_table.entity_id',
            array()
        )
        ->where('_gallery_table.value IS NULL');
    

    【讨论】:

    • 我已经更新了问题,添加了查询的外观,但我不清楚如何构建它...
    • 相应地更新了我的答案
    • 谢谢!!这似乎有效!我觉得还是有一些细节的,但是现在我可以得到一个基本上是我需要的集合了。
    【解决方案2】:

    相同的答案,但对于 magento 2:

    use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
    ........
    /**
     * @var CollectionFactory
     */
    public $productCollectionFactory;
    ........
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->getSelect()
        ->joinLeft(
            ['gallery_table' => $collection->getTable('catalog_product_entity_media_gallery_value_to_entity')],
            'e.entity_id = gallery_table.entity_id',
            []
        )
        ->where('gallery_table.value_id IS NULL');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多