magento select attribute by using where condition
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('modellijn')
->addAttributeToFilter('brand', 114)
->getFirstItem();
Hi there. I would 开发者_运维技巧like to know the right syntax for the following:
Select the attribute modellijn
where the attribute brand=114
. The above syntax gives back an error. I have been searching for 2 days now for the right syntax but no result so far unfortunately.
I am hoping someone here is willing to help me!
So the first problem I see here is... that you are attempting to select the attribute for a particular brand. Attributes do not in fact have brands (nor modellijns).
Can you please clarify the use case? Are you trying to get the modellijn (that's fun to type) value of all products with brand 114? What is your expected output?
Or, if you're more comfortable as such, what would be the SQL query you'd expect to see generated?
Thanks, Joseph Mastey
Okay, based on your update, I just wanted to clarify a few things.
- Attribute sets have attributes
- Attributes have options (sometimes)
- Products have attribute values
- Categories have products
If you just need to find the modellijin of a particular product, then you just need to ask for it. If you have a single product, this should do the trick:
$product = Mage::getModel("catalog/product")->load($id); // Magento does this for you in some cases
$product->getModellijn(); // this will return your value
$product->getAttributeText('modellijn'); // IIRC, this works for <select> type attributes
If you're loading products from a collection, you need to make sure to tell Magento that you want to load that attribute too. Selecting everything from EAV is even more expensive than selecting everything from a standard, normalized database. Therefore, Magento expects you to tell it what you need.
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("brand", 114); // limit for brand
$collection->addAttributeToSelect("modellijn"); // * also works but is slow
foreach($collection as $product) {
$product->getModellijn(); // just as above
$product->getAttributeText('modellijn');
}
Let me know if that does it for you. If it doesn't, please amend your post above to include a more complete SQL statement, and if possible more information about where you are using this data. That will help me understand the context within which you're executing the code.
精彩评论