I've tried removing an eav_entity_attribute using this code:
$entAttr = Mage::getModel('eav/entity_attribute')->getCollection()
->addFieldToFilter('attribute_set_id',$set->getId())
->addFieldToFilter('attribute_id',$attId)->getFirstItem();
$entAttr->delete();
but it doesn't work. I receive this error:
Column not found: 1054 Unknown column 'attribute_set_id' in 'where clause''
How can I delete an eav_entity_attribute this way?
Best Answer
The short version
Your error message isn't popping up due to your
delete
method call, it's popping up due to your collection use. For reasons lost to the mystery and smog of Los Angeles, theeav/entity_attribute
resource model class is initialized with theeav/attribute
resource string.This means the following collection
Actually queries the
eav_attribute
table.@DavidTay was actually on the right track. Whenever you're in doubt about how to do something in Magento, look at how the core team themselves did it. However, while looking at the admin console code for this will lead you to a method for removing your attribute from a attribute set, it's even better to look at API implementation code. This API code has an implicit promise of doing things in a stable way, where a lot of the early admin console code shows the scars of having been developed rapidly.
If you take a look at the
removeAttribute
implementation for the attribute set api class, you'll find your answer.Parsing this code out from it's API error checking — first you load an
eav/entity_attribute
model by it's attribute id.Remember, for reasons we don't know, this actually loads data from the
eav_attribute
table because of what's in the resource model_construct
.Next, we set the attribute set id on the
eav/entity_attribute
model.Then, we call the object's
deleteEntity
method, which actually removes the data from the correct table (eav_entity_attribute
)If you trace the
deleteEntity
method to the modeland then to the resource model
you can see that ultimately, Magento is using a DELETE query with the write adapter to remove the row.
Rather than do this yourself every-time, you can just call the API method directly. Not via XML-RPC or SOAP, but by manually instantiating the API implementation class