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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 677
    Comment on it

    getModel() method :

     

    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.
     
    For example:- 
     

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

    The above code tells to load the Mage_Catalog_Model_Product class.The method's parameter i.e., ‘catalog/product’ tells the module's model name to load.

    Now the model call can be specific by loading a product as : 

    $product = Mage::getSingleton('catalog/product')->load(786);

    Same way after loading the product we can fetch the attributes from the object of the model created in our case it is $product.
    lest we need to get the id of the product we can write using get method as :

    $id = $product->getId();

    similarly for product name, 

    $name = $product->getName();


    getData() Method :

     

    This method is used to get the relavent data from the object. 
    Lest we need to know if the product is enabled or disabled for the same we can use getData() method as :

    $product = Mage::getSingleton('catalog/product')->load(786);
    $status =$product->getData('status');

     

    In the above code we are loading the product of id 786 with the help of the Singleton method and then fetching the status of the product with the help of getData() method by passing the attribute name 'status'.

     

    In the same we can use getData() to fetch the product description,

    $description = $product->getData('description');

     

 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: