Pages

Monday, 23 June 2014

Magento Developper’s Guide (Lesson 14) – Make an update of magento/your plugin

This tutorial is the 13th of many tutorials . If you have not read the first articles yet, I strongly advise you to do so.

Today I will teach you how to use the automatic update of your plugin via the installer. As you know now, in the etc./config.xml of your plugin you have a version number. Magento when it is called, check the version number and detects if your plugin is up to date. It checks in the table core_resource the version number of your plugin installed on your site and if it does not match your current plugin, it launches the installer.

The installers of your plugin will be in the /sql/test_setup/, the first is an « install » it will be called

 mysql4-install-0.1.0.php

Then the others will be « upgrade » ones and they will call mysql4-upgrade--.php example:
The first update to version 0.1.0 to version 0.1.1 is called:

 
mysql4-upgrade-0.1.0-0.1.1.php
 
The following will be:

 
mysql4-upgrade-0.1.1-0.1.2.php
 
Etc. ..

Declare our setup folder

First start by creating a folder /sql/test_setup at the root of your plugin folder.
Then go to your /etc/config.xml <global> <ressources> then add:

<easylifetest_setup>
    <setup>
        <module>Easylife_Test</module>
        <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
    </setup>
    <connection>
        <use>core_setup</use>
    </connection>
</easylifetest_setup>
 
Your file is now declared and your install and upgrade files will be taken into account at the next call of a magento page.

Add an attribute in the product page

We will now see what looks like our install by adding a field in the product view (which will be automatically included in the admin).

So create the file mysql4-install- 0.1.0.php in your plugin in the /sql/test_setup/

<?php
$this->startSetup();
$this->addAttribute('catalog_product', 'my_field', array(
    'group'           => 'My Field',
    'type'            => 'int',
    'label'           => 'My Field',
    'input'           => 'select',
    'source'          => 'eav/entity_attribute_source_boolean',
    'backend'         => '',
    'frontend'        => '',
    'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'required'        => false,
    'default'         => '0',
    'user_defined'    => 0,
    'apply_to'        => 'simple',
    'note'            => '',
    'visible'         => true,
    'is_filterable_in_search', '0',
    'used_in_product_listing', '1',
));
$this->endSetup();

As you can see, you do what you want in your install as long as it starts with $this->startSetup(); and it ends up with $this->endSetup();

for the function addAttribute, we will not explain it in this tutorial;)
The upgrade file is the same:

<?php
$this->startSetup();
//here you can put whatever you want
$this->endSetup();

Here we are! It’s the end of this tutorial. I hope it will be able to help some of you and I invite you to ask questions, put a « +1″, love it on Facebook, or leave messages in the comment.

You save time with this tutorial? Take 2min to help me and to link to this article on your blog, Facebook, twitter or Google+ … this is not much for you but for me it’s huge! Thank you very much to follow me and help me.

Find the summery of this tutorial

No comments:

Post a Comment