Magento ORM等同于“产品的SELECT DISTINCT属性”?


8

我需要在(伪)SQL中检索用于特定产品属性的所有值的列表:

SELECT DISTINCT attribute FROM products;

我将如何使用Magento ORM生成等效查询?我已经试过了该distinct()功能,但是它没有达到我的期望:

// Returns an array of NULL with a length equal to all products in the catalog
Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('attribute')
            ->distinct(true)
            ->getColumnValues('attribute');

我正在努力获得的将是一组attribute值,没有重复项

array('some value', 'some other value', 'a really common value', 'etc...');

您是否正在寻找所有类型的属性的值?还是跌落?
拉贝阿

Answers:


1

感谢kalpesh,这已经是博客了:

http://ka.lpe.sh/2011/06/06/magento-get-all-the-values-of-a-magento-eav-for-a-particular-attribute-code/

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); //here, "color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
    $myArray[$instance['value']] = $instance['label'];
}
Mage::log($myArray);

这是另一个解决方案:https : //stackoverflow.com/a/15509714/1480397

但是我不确定这是否适用于非选择属性。


是的,它仅适用于选择属性,但对于这些属性,这是必须的方法。
Fabian Schmengler,2015年

1

您可以直接通过连接提交任何SQL语句,以获取无法通过magentos的api访问的任何数据。

$db_resource = Mage::getSingleton('core/resource');
$db_connection = $db_resource->getConnection('core_write');
$sql = sprintf("SELECT DISTINCT attribute FROM `%s`", $db_resource->getTableName('product'));
$dataset = $db_connection->fetchAll($sql);

查询函数为fetchRow和fetchAll,其结构如下:

fetchAll($ structured_sql,$ bind_filters = array(),$ fetchMode = null)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.