Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of set and get methods in magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 632
    Comment on it

    Magento handles the setting and getting values of the fields with the set and get methods.
    To understand how it handles using these methods lets see :

    the set method in magento works as setter method, which sets to the value of the attributes and the get method works as getter method, which gets the value of the attribute or sometimes used to load the model, block and helper.

    Lets load a model with the get method first,

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

    In the above code we have loaded the product with the use of the getModel method same way getBlock, getHelper methods can be used to load the block and helper respectively. Now, When we print the $product we will get the array of product.
    The array containing the attribute of the product with values.
    Now the thing is if we are required to get the value of any attribute we can fetch it using get method as getAttributeName() replace the attribute name with the name of the product attribute of which  we need to get the value.

    Same way if we are required to set the value of the attribute we can use the set method of the magento to set the value of it.
    We can do it as setAttributeName() here also we can replace the AttributeName as the name of the product attribute of which we want to set the value.


    Lets take example of both,

    for the get method we can get the value as,

    $product_name = $product->getName();
    $product_MetaDescription = $product->getMetaDescription();

    The above statements are fetching the product name and the meta description of the product loaded.

    Now for the set method we can set the value as,

    $product->setName('New Name');

    The above statement is to set the "name" attribute value with the custom value "New Name". It sets the product name as 'New Name'. now if we want to change the name of the product permanently we can use save function to save the value as :

    $product->setName('New Name');
    $product->save();

    it will update the value of the product name in database.

    In these ways we can use the set and get methods in magento.

 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: