Find the attribute_id by attribute_code
-
16-10-2019 - |
سؤال
I need to find the attribute_id
values of the attributes image
, small_image
and thumbnail
. I know them for my database - 85, 86 and 87, but I need to make my query dynamic, instead of with hard-coded values. What I struggle with is to find in which table the attributes are stored. I checked catalog_product_attribute
but there is no column with the name/code
of the attributes.
I need to acquire them as an SQL query, not as Mage::...
PHP code.
A sample SQL query code or any other guidance will be very helpful.
المحلول
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode);
$id = $attribute->getId();
if $id
is null
then the attribute does not exist.
It works the same for getting category attributes, customer attributes and customer address attributes.
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_category', 'is_anchor');
$attribute = Mage::getModel('eav/config')->getAttribute('customer', 'gender');
$attribute = Mage::getModel('eav/config')->getAttribute('customer_address', 'postcode');
[EDIT]
Apparently I can't read. I missed the line that stated sql query
.
Here is a query also:
SELECT
e.attribute_id
FROM
eav_attribute e
LEFT JOIN
eav_entity_type t
ON e.entity_type_id = t.entity_type_id
WHERE
e.attribute_code = 'image' AND
t.entity_type_code = 'catalog_product'
نصائح أخرى
I used this codes for getting the attribute ID in eav_attribute tables
$attribute_code = 'image';
$eavCollections = Mage::getModel('eav/entity_attribute')->getCollection()
->addFieldToFilter('attribute_code', array('eq'=> $attribute_code ));
foreach($eavCollections as $eavCollection){
$attribute_id = $eavCollection['attribute_id']; //get the attribute id
echo $attribute_id;
}