Search code examples
magento

Replace the column name in the magento collection while loading it


I have a custom module and I am loading a collection like following

$collection = Mage::getModel('module/product')->getCollection()
                                   ->addFieldToFilter('sku',$sku);

There is field named as prod_id in the database. Can I get this as entity_id while loading the collection?

If yes. Please help how to do this.


Solution

  • First of all all

    addAttributeToFilter()  is used to filter EAV collections.
    
    addFieldToFilter() is used to filter Non-EAV collections.
    

    EAV-models are for example product, customer, sales, etc so you can use use addAttributeToFilter() for those entities.

    addFieldToFilter() is mapped to `addAttributeToFilter()` for `EAV` entities. So you can just use `addFieldToFiler().`
    

    You can have a look in app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php where the Mapping is done:

    public function addFieldToFilter($attribute, $condition = null) {
        return $this->addAttributeToFilter($attribute, $condition);
    }
    

    If you are using custom module then you can directly use addFieldToFilter() with your column name

    Like

    $collection = Mage::getModel('module/model')->getCollection()
                                       ->addFieldToFilter('column_name',$data);
    

    Let me know if you have any query