Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • getModel() and getResourceModel() in magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.82k
    Comment on it

    Magento have two parts first as the model, with the Model it handles the business-logic, as Entity Attribute Value (EAV) Model, and the another Resource part, which only deals with talking to the database.

     

    The collections in magento are the collections of the models, when we need to get result from two model we use collections. 

     

    Mage::getModel()

     

    Mage::getModel() will create a new instance of an object each time even such object exists in configuration.
    getModel will always return a new instance of the requested model every time.Mage::getModel() is used when you want to have the fresh data from the database. Example is when you want to show records from database.

    Example :

     

    $product1 = Mage::getModel('catalog/product');
    $product2 = Mage::getModel('catalog/product');

    $product1 and $product2 both are different instants with different memory allocation.

    Models in magento contains a reference to a resource model which loads the data, i.e. A Product Model has a Product resource model. A model object can be used to call the collection object.It can be done as :

     

    $product1 = Mage::getModel('catalog/product')->getCollection()->load(145);


    Mage::getResourceModel()

     

    All collections in Magento are resource models. They are instantiated by Mage::getResourceModel() or by Mage::getModel()->getCollection()


    Models in magento don't have database connection directly instead the resource models links directly with the database adapter


    Resource models contains code which fetches data from a data store. A collection contains a code to fetch the group of model objects.


    It is instantiated with the Mage::getResourceModel method.

    Example : 

     

    Mage::getResourceModel('catalog/product_collection')->load(145);
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: