tags:

views:

113

answers:

1
$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!

A: 

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.

Joseph Mastey
the normal sql would be something like:"SELECT MODELLIJN FROM .... WHERE BRAND_ID=114" ....I have a lot of products and we have been adding attributes to the each category of products.Basicly i know what the brand_id of some product is. But now i would like to know what the Modellijn (modelline) of that product is.so for example. my brand is Bentley for breitling air with the no.114 value and the modellijn(modelline) for this product is breitlingHope u can help me...cos this eav model is very much confusing me!
Paul de Zwaan
modellijn and brand are both attributes that belong to the product
Paul de Zwaan