Ответ 1
Я считаю, что вы не пытаетесь прочитать значение атрибута модели продукта, но получите список всех используемых значений для определенного атрибута.
"Обычные" атрибуты
Обычные атрибуты - это все атрибуты, которые не используют select
или multiselect
для ввода, но поле text или textarea или что-то подобное.
Для этих атрибутов используйте это:
// specify the attribute code
$attributeCode = 'name';
// build and filter the product collection
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter($attributeCode, array('notnull' => true))
->addAttributeToFilter($attributeCode, array('neq' => ''))
->addAttributeToSelect($attributeCode);
// get all distinct attribute values
$usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
Атрибуты "Option"
Если это атрибут select
или multiselect
, вам все равно нужно сопоставить идентификатор параметра с параметрами. Это тот случай, если вы получаете список целых чисел вместо человеческих читаемых меток (например, для атрибута color
или manufacturer
).
// specify the select or multiselect attribute code
$attributeCode = 'color';
// build and filter the product collection
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter($attributeCode, array('notnull' => true))
->addAttributeToFilter($attributeCode, array('neq' => ''))
->addAttributeToSelect($attributeCode);
$usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
// ---- this is the different part compared to the previous example ----
// fetch the attribute model
$attributeModel = Mage::getSingleton('eav/config')
->getAttribute('catalog_product', $attributeCode);
// map the option id to option labels
$usedAttributeValues = $attributeModel->getSource()->getOptionText(
implode(',', $usedAttributeValues)
);
// $usedAttributeValues now contains an array of used values in human readable format
Пример запроса прямого DB
В зависимости от того, где вы хотите это сделать, приведен пример получения значений без использования коллекции продуктов. Это немного более эффективно.
Используйте только следующий код в моделях ресурсов, так как это касается кода, связанного с DB.
Это подразумевается как образовательный пример, показывающий, как работать с таблицами Magento EAV.
// specify the attribute code
$attributeCode = 'color';
// get attribute model by attribute code, e.g. 'color'
$attributeModel = Mage::getSingleton('eav/config')
->getAttribute('catalog_product', $attributeCode);
// build select to fetch used attribute value id's
$select = Mage::getSingleton('core/resource')
->getConnection('default_read')->select()
->from($attributeModel->getBackend()->getTable(), 'value')
->where('attribute_id=?', $attributeModel->getId())
->distinct();
// read used values from the db
$usedAttributeValues = Mage::getSingleton('core/resource')
->getConnection('default_read')
->fetchCol($select);
// map used id to the value labels using the source model
if ($attributeModel->usesSource())
{
$usedAttributeValues = $attributeModel->getSource()->getOptionText(
implode(',', $usedAttributeValues)
);
}
// $usedAttributeValues now contains an array of used option value labels